0

I'm new to python and I'm trying to write a web scraping script. I'm trying to double click on this element (it's neither a button nor link - just a td element) and am having trouble with even finding it in the first place.

Below is the code

<td align="left" valign="middle" class="   "
title="Business Profile (Imported)">Business Profile (Imported)</td>

When I select it, the class changes. I suspect this is where the problem is at

<td align="left" valign="middle" class="  cellselected "
title="Business Profile (Imported)">Business Profile (Imported)</td>

I used css selector & xpath. None works. I tried both:

driver.find_element_by_xpath('//td[@title="Business Profile (Imported)"]').click()
driver.find_element_by_css_selector("td[title='Business Profile (Imported)']")

This is the error I get:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//td[@title="Business Profile (Imported)"]"}

Any help would be tremendously appreciated. Thanks!!

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

2 Answers2

0

To locate the desired <td> element you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • XPATH 1:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[contains(@title, 'Imported') and starts-with(., 'Business Profile')]")))
    
  • XPATH 2:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[starts-with(@title, 'Business Profile') and contains(., 'Imported')]")))
    
  • 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
  • What does your answer provide that Kunduk's doesn't? Other than a locator that is less specific and confusing??? Why are you checking that the title contains 'Imported' but that the contained text contains 'Business Profile'... why not just check either the title or the contained text? – JeffC Aug 08 '19 at 14:53
-1

To handle dynamic element induce WebDriverWait and element_to_be_clickable and use the following xpath

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//td[@title='Business Profile (Imported)' and text()='Business Profile (Imported)']"))).click()

OR

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//td[@title='Business Profile (Imported)'][contains(.,'Business Profile (Imported)')]"))).click()

You need to imports following to execute above code.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Your locator probably doesn't need to check both the title AND the contained text. I would assume that either would be enough. – JeffC Aug 08 '19 at 14:51