0

Page + HTML:

Elements Page Menu List

In the menu item on the left, I have 9 menu items in the list. I am able to get selenium to click on the first 4, up until the id=item-3.

But after and including id=item-4, Selenium just can't click, and gives me a No Such Element Exception.

I am clicking like standard:

element = driver.find_element(By.ID, "item-3")
element.click()

Why can I not do:

element = driver.find_element(By.ID, "item-4")
element.click()

and through to item-8?

I thought at first I need to scroll down, because maybe it also needs to be visible on screen:

My page view

But I tried to scroll and it still didn't work.

Am I missing something obvious? I tried XPATH, but it also gives the same results.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

2 Answers2

0

My first step for debugging missed elements is to check in Chrome, to see what the browser sees

F12 > Elements > Ctrl+F

Type in #item-4 and see if the browser sees it

This works for 1920 x 1080

Is the screen size smaller?

gords
  • 51
  • 7
  • Tried that now. I refreshed the page, and search in the dev tools for "#item-4", and it found it with no problems – Thomas Bates Mar 08 '23 at 17:44
  • yes I am on a small screen right now whilst I am in a black out. Would it fix the issue if I ran it in a headless webdriver? – Thomas Bates Mar 08 '23 at 17:51
  • Ok I tried this on a bigger laptop screen and it worked. Why would the screensize have an issue? – Thomas Bates Mar 08 '23 at 18:07
  • The bigger screen sees more of the menu, it's good practice to set the screen to 1920 x 1080 after the driver is created, if headless – gords Mar 08 '23 at 18:25
0

If you look at your error trace, it is giving you below error. This error tells you that selenium was not able to click on desired element due to the various validations selenium performs before clicking.

Message: element click intercepted: Element <span class="text">...</span> is not clickable at point (112, 628)

Solution: In such cases, you should try to to perform click() using Javascript.

Below is the code using JavaScript(and not selenium's click()) to click on Buttons:

element = driver.find_element(By.XPATH, "//span[text()='Buttons']")
# element = driver.find_element(By.ID, "item-4") # you can use even this locator
driver.execute_script("arguments[0].click();", element)

Full working code:

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://demoqa.com/elements")
element = driver.find_element(By.XPATH, "//span[text()='Buttons']")
driver.execute_script("arguments[0].click();", element)

Result:

Process finished with exit code 0

enter image description here

References: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html

Shawn
  • 4,064
  • 2
  • 11
  • 23
  • Can I ask you to please explain the "driver.execute_script("arguments[0].click();", element)" part? – Thomas Bates Mar 08 '23 at 17:43
  • Check my updated answer for explanation of the solution. Feel free to ask if you have queries. Let me know if it works. – Shawn Mar 08 '23 at 17:50