mete BEDER
mete BEDER

Reputation: 51

Python Selenium Get Text by Its Color

i need to get a specific text from a website by the text color, in my case this color is rgb(209,196,233).

The text element in HTML:

<span style="color: rgb(209, 196, 233);">−14.64</span>

my code :

XYZ = driver.find_element(By.XPATH, "//span[@style='color: rgb(209,196,233);']").text

When i execute, the code crashed with this error :

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@style='color: rgb(209,196,233);']"}

What did i do wrong? :(

Upvotes: 0

Views: 510

Answers (1)

Furkan Ozalp
Furkan Ozalp

Reputation: 334

I think your XPath is correct. But as the error says: Unable to locate element. You can use wait to "wait" until your element is located. For more, you can see: https://selenium-python.readthedocs.io/waits.html

So you can do that as follows:

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

#..your code for webdriver
#wait
wait = WebDriverWait(driver, 10)

#After that get text from xpath
XYZ = wait.until(EC.presence_of_element_located((By.XPATH, 
"//span[@style='color: rgb(209, 196, 233);']"))).text

Upvotes: 1

Related Questions