0
votes

I am trying to get this number (-22.65) that changes in the website Page code

I have tried with the xpath, with contain text...

text = driver.find_element_by_xpath("//*[contains(text(),'valueValue-2KhwsEwE')]").text
print(text)

But in any case I get the number itself... I get the following message:

File "C:\Users\ESJOMAN2.spyder-py3\temp.py", line 22, in
text = driver.find_element_by_xpath("//*[contains(text(),'valueValue-2KhwsEwE')]").text

File "C:\Users\ESJOMAN2\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)

File "C:\Users\ESJOMAN2\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {

File "C:\Users\ESJOMAN2\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)

File "C:\Users\ESJOMAN2\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)

NoSuchElementException: no such element: Unable to locate element:
{"method":"xpath","selector":"//*[contains(text(),'valueValue-2KhwsEwE')]"}
(Session info: chrome=89.0.4389.90)

Any ideas?

2
Changes how? In relation to the page. Also the -2KhwsEwE seems to be dynamic just remove it. - Arundeep Chohan
it can't find the element with that locator. Perhaps try a wait. - DMart

2 Answers

0
votes

Try this:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
try:
    text = WebDriverWait(driver, 20).until(
        EC.presence_of_element_located(
            (By.XPATH, 'ELEMENT XPATH'))
    )
except:
    driver.quit()

print(text.text)
0
votes

Try to use something like that:

text = driver.find_element_by_xpath("//div[contains(text(),'sourcesWrapper')]//div[contains(),'valueValue']").text

or

text = driver.find_element_by_xpath("//div[contains(text(),'sourcesWrapper')]//div[contains(),'valueValue']").get_attribute('value')

before that, use

time.sleep(5)

so code snipped looks like:

time.sleep(5)
text = driver.find_element_by_xpath("//div[contains(text(),'sourcesWrapper')]//div[contains(),'valueValue']").text
print(text)

By the way, have you checked, might your elements inside iframe, did iframe locate on your page?