0

I'm trying to identify the following element but no matter the method it doesn't see it.

<span onclick="onClickTab('details'); return false;" id = "details" name = "details" class style ="display: inline;"">...</span>

I've tried with: Xpath, relative xpath, onclick, onclick contains, by id, by name, just nothing works. It is a clickable button which appears after selecting an item in a list.

Current code is:

try:
WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//span[@onclick='onClickTab('details'); return false;']")))
except
print("Error")

driver.find_element_by_xpath("//span[@onclick='onClickTab('details'); return false;']").click()

if there are any minor syntax problems like a "(" or such it might be because I typed it by hand, that shouldn't be the issue. I'm forever grateful if you could point me to the right direction.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
IceSC
  • 1
  • 1
  • 2
  • You should be able to ask the browser what the button's exact xpath is. Usually this is done by right-clicking the button and choosing "inspect". – John Gordon Jun 30 '20 at 19:27
  • It's the only place in the webpage where I can't right click. I have gone through the code to find that reference to it. – IceSC Jun 30 '20 at 19:43
  • I've also tried with the devtools CTRL+Shift+C and going to the element. Neither css selector nor the xpatjh are working. The same error is given: " Unable to locate element" – IceSC Jun 30 '20 at 19:56

3 Answers3

0

I was able to select the element using the following xPath:

//span[contains(@onclick, "onClickTab(\'details\'); return false;")]

Using selenium, I used:


try:
    WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH,'//span[@onclick="onClickTab(\'details\'); return false;"]')))
except:
    print("Error")

driver.find_element_by_xpath('//span[@onclick="onClickTab(\'details\'); return false;"]').click()
zmike
  • 1,090
  • 10
  • 24
  • Doesn't work :( . I've tried every possible combination. I was able to acces every element until now but this one. Could it be something that is locked into a frame? But I think I did select the frame of the entire page. – IceSC Jun 30 '20 at 20:54
0

To click on the <span> element instead of presence_of_element_located() you have 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, "span#details[name='details'][onclick^='onClickTab']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='details' and @name='details'][starts-with(@onclick, 'onClickTab')]"))).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
0

Here i suggest you to use id in Xpath.

driver.find_element_by_xpath("//span[contains(@id,'details')]").click()

if multiple element is there then you have to use foreach loop of driver.find_elements_by_xpath("//span[contains(@id,'details')]") and check with text and click on match element.

Amar Patel
  • 39
  • 5