0

I am trying to click on a span class located inside a div class. Here's the HTML:

<div class="modal-content scrollbar">
  <div class="block block-always-show action-black-box waves-effect">
    <div class="icon xray-icon"></div>
    <span class="txt">Xray - Test Product Research</span>
</div>

Still learning Selenium but here's what I've tried:

driver.find_element_by_xpath("//span[contains(@class, txt) and contains(text()='Xray - Test Product Research')]").click()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Xray - Test Product Research']"))).click()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='txt' and contains(.,'Xray - Test Product Research')]"))).click()

I am getting these errors:

NoSuchElementException: Message: no such element: Unable to locate element: 

and

TimeoutException: Message: 

Thanks in advance and appreciate any help on a solution.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Alexis
  • 89
  • 2
  • 9
  • Could you post the url of your page? – jizhihaoSAMA Jul 18 '20 at 05:28
  • 1
    check if your element is present within the iframe – SeleniumUser Jul 18 '20 at 07:50
  • Could be a problem with XPath. Can you paste this in console and see if you get any result `$x("//span[contains(@class, txt) and contains(text()='Xray - Test Product Research')]")` if not try this `$x("//span[@class='txt' and contains(text(),'Xray')]")` Note: as mentioned in the above comment, make sure this elemet is not inside a iframe. – Muditha Perera Jul 18 '20 at 08:57

2 Answers2

1

To click on the element with text as Xray - Test Product Research 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, "div.modal-content.scrollbar span.txt"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='modal-content scrollbar']//span[@class='txt' and contains(., 'Xray - Test Product Research')]"))).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
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You're on the right path but it looks like your xpath's just need some slight tweaking. Here are 2 examples that should find the element in question:

This xpath requires the exact text contained in the span tags:

//span[contains(@class, 'txt') and text() = 'Xray - Test Product Research']

This next xpath accepts snippets of text between the span tags:

//span[contains(@class, 'txt') and contains(text(), 'Xray')]

You may still need a small wait/time delay to allow for the elements to load on the page before trying to click on them

SandstormNick
  • 1,821
  • 2
  • 13
  • 24