0

How can I click via selenium and python on such element:

      <a href="#create" class="btn toolbarBtn">
          <i class="fa fa-plus-circle"></i> Create
        </a>

When I Copy Xpath this element from Chrome i got this:

//*[@id="page"]/div/div/div/div[1]/div[1]/a/i

But when I use it in my code:

driver.find_element_by_xpath('//*[@id="page"]/div/div/div/div[1]/div[1]/a/i').click()

I got error that:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="page"]/div/div/div/div[1]/div[1]/a/i').click()"}

When I tried to use find_element_by_link_text and partial_link_text I got same error that selenium was unable to find such element.

2
  • Are you sure about the xpath?is it really valid? could you please paste the link of the page here? Commented Mar 30, 2017 at 11:14
  • I can't paste link here because its under corporate network. But I think I found solution. Selenium is too fast, and I need to put implicitly_wait(10) somewhere in my code :) Commented Mar 30, 2017 at 14:45

3 Answers 3

3

Try to wait until element becomes clickable:

from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href='#create']"))).click()

If this also doesn't work check whether your link located inside <iframe>. If so, you need to switch to that frame before clicking link:

driver.switch_to.frame("Put frameID here")
Sign up to request clarification or add additional context in comments.

Comments

0

In this particular case I found that selenium is going too fast, so some elements doesn't have time to load. When I put sleep(5) in the code the errors gone.

To avoid putting too many sleep() in my code I use driver.implicitly_wait(10) which resolved my problem definitely. I also dense my code a lot throwing up nearly all sleep() functions.

Thanks all for help!

5 Comments

Please use the Post answer button only for actual answers. You should modify your original question to add additional information.
This is a less than ideal solution compared to using Selenium's built-in wait() function.
And why driver.implicitly_wait(10) is worse than explicitly waiting?
@heniekk, ExplicitWait provides with more options and flexibility. Check this stackoverflow.com/questions/10404160/…
@Andersson thank you for this. This information is very valuable for me. I marked your answer as approved. Update: but still I found Chloe comment here stackoverflow.com/questions/10404160/… valid in my situation. Implicite wait is much easier to use for me.
-1

please try below code

driver.find_element_by_partial_link_text("Create").click()

3 Comments

There is no method find_element_by_partial_link() in selenium. Also there is no need to use search by partial link text as search by link text "Create" is more precise and it should also find the link
It should be find_element_by_partial_link_text
Sorry I didn't mention this (I edited my question to address this), but I tried this one to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.