Home > Back-end >  How to find div element by text with python selenium?
How to find div element by text with python selenium?

Time:10-12

I have to find this text and select. enter image description here

I tried :

element = driver.find_element_by_xpath('//*[@id="product-detail-app"]/div/div[3]/div[1]/div[2]/div[3]/div[2]/div[3]').text
print(element)

But it gives error:

---------------------------------------------------------------------------
NoSuchElementException                    Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11408/4199675862.py in <module>
      9 search_bar.send_keys(Keys.RETURN)
     10 result = driver.find_element_by_class_name("prdct-desc-cntnr-name").click()
---> 11 price = driver.find_element_by_xpath('//*[@id="product-detail-app"]/div/div[3]/div[1]/div[2]/div[3]/div[2]/div[3]')
     12 print(element)
     13 #for i in browser.find_elements_by_class_name("sp-itm"):

c:\users\seher\appdata\local\programs\python\python37\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element_by_xpath(self, xpath)
    392             element = driver.find_element_by_xpath('//div/td[1]')
    393         """
--> 394         return self.find_element(by=By.XPATH, value=xpath)
    395 
    396     def find_elements_by_xpath(self, xpath):

c:\users\seher\appdata\local\programs\python\python37\lib\site-packages\selenium\webdriver\remote\webdriver.py in find_element(self, by, value)
    976         return self.execute(Command.FIND_ELEMENT, {
    977             'using': by,
--> 978             'value': value})['value']
    979 
    980     def find_elements(self, by=By.ID, value=None):

c:\users\seher\appdata\local\programs\python\python37\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

c:\users\seher\appdata\local\programs\python\python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="product-detail-app"]/div/div[3]/div[1]/div[2]/div[3]/div[2]/div[3]"}
  (Session info: chrome=94.0.4606.71)

I tried different solutions but they didn't work. This is a selectable div. I have to find the text I need and set it selectable. But I can't find the text. Please help me, thanks.

CodePudding user response:

Try adding a wait before accessing the element, something like this:

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

wait = WebDriverWait(driver, 20)


value = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="product-detail-app"]/div/div[3]/div[1]/div[2]/div[3]/div[2]/div[3]'))).text
print(value)

Also your XPath locator looks very bad. In can be highly unreliable.
UPD
The better XPath locator for that element can be //div[@class='variants']//div[text()='38'] so your code will be:

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

wait = WebDriverWait(driver, 20)


value = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='variants']//div[text()='38']"))).text
print(value)

UPD
Seems you have to switch to the new window.
Try this:

new_window = driver.window_handles[-1]
driver.switch_to_window(new_window)

To get back to the first window you will have to do this:

old_window = driver.window_handles[0]
driver.switch_to_window(old_window)
  • Related