0

I'm using selenium to automate some tasks with webdriver.

Turns out I can't find a div to click on, selenium just can't find it.

Does anyone have a suggestion?

HTML :

<div aria-controls="leftAdvPnl_body" aria-expanded="false" aria-haspopup="true" class="rich-stglpanel-header " id="leftAdvPnl_header" onclick="SimpleTogglePanelManager.toggleOnClient(event,'leftAdvPnl');" onkeypress="return keypressclickhandle(event);" role="link" tabindex="0"><div aria-hidden="true" class="rich-stglpanel-marker"><div class="rich-stglpnl-marker" aria-hidden="true" id="leftAdvPnl_switch_on" style="display: none">«</div><div class="rich-stglpnl-marker" aria-hidden="true" id="leftAdvPnl_switch_off">»</div></div><span id="leftAdvPnl_header_label">Pesquisar</span><span aria-hidden="true">&nbsp;</span></div>

Code phyton:

while len(navegador.find_elements_by_xpath('//*[@id="leftAdvPnl_header"]')) < 1:
    time.sleep(1)
    print("Procurando formulário do processo")

link=navegador.find_element_by_xpath("//*[@id="leftAdvPnl_header"]")
link.click()

Thanks!

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Please try to use `find_element_by_css_selector` instead of the `find_element_by_xpath` and see if the issue persists. – unityJarvis Nov 05 '21 at 01:13
  • I tried: while len(browser.find_elements_by_css_selector('span#id_leftAdvPnl_header_label')) < 1: time.sleep(1) print("Looking for process form") – Diogo Nature Nov 05 '21 at 01:30

4 Answers4

0

You have 2 nested double quotes, instead of "//*[@id="leftAdvPnl_header"]" use '//*[@id="leftAdvPnl_header"]'

Pedro Maia
  • 2,666
  • 1
  • 5
  • 20
  • No influence. Selenium cannot find this div. It has no code error. – Diogo Nature Nov 05 '21 at 00:57
  • Would you mind sharing the link to the site? – Pedro Maia Nov 05 '21 at 01:01
  • I would like to, but it's an application that can only be accessed by digital certificate. Is there any way to do this while preserving personal data? – Diogo Nature Nov 05 '21 at 01:19
  • @DiogoNature Welcome to the life of automation, sometimes websites just want to be difficult. We are building an automation tool called Roboteur which will allow you to automate testing processes without writing a single line of code. Its an automation tool, so there is way more to do with it than just automating test cases. Check it out. https://spacepencil.co.uk – Mini-Man Nov 05 '21 at 07:12
0

You need to wait for an element to be clickable, so try:

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

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="leftAdvPnl_header"]'))).click()
Whispored2001
  • 411
  • 1
  • 5
0

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

xpath that you should check :

//div[@id='leftAdvPnl_header']

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

If we have 1/1 matching node, Please make sure that :

  1. This div is not under an iframe.
  2. This div is not under a shadow-root.
  3. You should not be on new tab/windows launched by selenium.

Code :

try:
    WebDriverWait(navegador, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='leftAdvPnl_header']"))).click()
    print("Clicked on web element")
except:
    pass

Imports :

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

Updated :

CSS are far more better locator than XPath, please try the below one :

div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']

Same way to check if we have 1/1 matching node or not.

To click, there are 4 ways in Selenium.

Code trial 1:

time.sleep(5)
driver.find_element_by_css_selector("div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']").click()

Code trial 2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']"))).click()

Code trial 3:

time.sleep(5)
button = driver.find_element_by_css_selector("div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_css_selector("div[onclick^='SimpleTogglePanelManager.toggleOnClient'][onclick*='(event,'][onclick*='leftAdvPnl']")
ActionChains(driver).move_to_element(button).click().perform()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Yes, xpatch is found. 1. There is only 1 iframe on the page, but it is not inside this element: 2. I need to study about it, I can't identify if it exists or not. 3. No, it's not a new tab. Apparently this platform was built with Jboss. – Diogo Nature Nov 05 '21 at 11:31
  • If xpath is unique, the you should try out my solution.. it should work. Let me know how it goes. – cruisepandey Nov 05 '21 at 11:42
  • I would very much like to have worked out, but no. Is it something related to the onclick="SimpleTogglePanelManager.toggleOnClient(event,'leftAdvPnl');" ? – Diogo Nature Nov 05 '21 at 12:54
  • Please see updated section above, I hope that will work. – cruisepandey Nov 05 '21 at 17:12
  • I tested all 4 codes and nothing, there must be something hiding this div, impossible. Log error: https://paste-bin.xyz/10678 – Diogo Nature Nov 05 '21 at 18:48
0

To click on the element with text as Pesquisar you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "div.rich-stglpanel-header#leftAdvPnl_header span#leftAdvPnl_header_label").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//span[text()='Pesquisar' and @id='leftAdvPnl_header_label']").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.rich-stglpanel-header#leftAdvPnl_header span#leftAdvPnl_header_label"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Pesquisar' and @id='leftAdvPnl_header_label']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352