Issue
I'm scraping a website and
I want to check if the content that is found by browser.find_element()
matches a specific word given in the code. If it does, it should do thing 1; if it doesn't, it should do thing 2.
Check = browser.find_element(By.XPATH, 'XPATH')
if Check == 'Target':
print('True')
else:
print('False')
Solution
browser.find_element(By.XPATH, 'XPATH')
Above will return an object of WebElement. If you want to store the content of an HTML node into a variable, you need to use .text
. Check the code below:
Check = browser.find_element(By.XPATH, 'XPATH').text
Example:
driver = webdriver.Chrome()
driver.get("https://stackoverflow.com/")
driver.maximize_window()
Check = driver.find_element(By.XPATH, "//span[@class='-img _glyph']").text
if Check == 'Stack Overflow':
print('True')
else:
print('False')
Answered By - Shawn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.