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.

heniekk
  • 374
  • 2
  • 10
  • Are you sure about the xpath?is it really valid? could you please paste the link of the page here? – Manoj Kengudelu Mar 30 '17 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 :) – heniekk Mar 30 '17 at 14:45

3 Answers3

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")
Andersson
  • 51,635
  • 17
  • 77
  • 129
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!

heniekk
  • 374
  • 2
  • 10
  • Please use the *Post answer* button only for actual answers. You should modify your original question to add additional information. – JeffC Mar 30 '17 at 16:38
  • This is a less than ideal solution compared to using Selenium's built-in wait() function. – Al Sweigart Mar 31 '17 at 03:34
  • And why driver.implicitly_wait(10) is worse than explicitly waiting? – heniekk Mar 31 '17 at 10:25
  • @heniekk, `ExplicitWait` provides with more options and flexibility. Check this http://stackoverflow.com/questions/10404160/when-to-use-explicit-wait-vs-implicit-wait-in-selenium-webdriver/28067495#28067495 – Andersson Mar 31 '17 at 11:59
  • @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 http://stackoverflow.com/questions/10404160/when-to-use-explicit-wait-vs-implicit-wait-in-selenium-webdriver/28067495#comment46445762_28067495 valid in my situation. Implicite wait is much easier to use for me. – heniekk Mar 31 '17 at 13:43
-1

please try below code

driver.find_element_by_partial_link_text("Create").click()
Alex Bruce
  • 533
  • 2
  • 10
  • 23
  • 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 – Andersson Mar 30 '17 at 11:49
  • It should be `find_element_by_partial_link_text` – Chanda Korat Mar 30 '17 at 12:25
  • Sorry I didn't mention this (I edited my question to address this), but I tried this one to. – heniekk Mar 30 '17 at 14:56