PythonCoder4_09
PythonCoder4_09

Reputation: 67

Best way to check if text exist or not in the page using Selenium- Python

I'm new to writing a test which verifies if text exists on a webpage in Python. I want to verify the words 'heading of the page' exist but my issue is there isn't any xpath or css selector. I've seen examples of people using those to find text on a particular page but my page doesn't have that for the words i'm searching for.

I want to verify the texts exists and then throw an exception if needed. Below is the stack trace of the google inspect of the text i want to verify and the code i've written so far but need help on structuring it correctly and how to find the text correctly.

Stack trace

<div style="width:161.75mm;min-width: 161.75mm;">heading of the page</div>

Below is the code i've started but it's failing with AssertionError

src = driver.page_source
text_found = re.search(r'heading of the page', src)
assert element.text == 'heading of the page'

Upvotes: 1

Views: 2304

Answers (1)

noahcoleman
noahcoleman

Reputation: 89

You could do a few things to solve this problem, using Xpath in both cases.

You can use text() like this //div[text()='your text'], this should return your element. Also, you can match the style using @style, like this //div[@style='width:161.75mm;min-width: 161.75mm;']

Here is a link to a great, useful Xpath cheatsheet!

If you're using selenium, it would work like this.

driver.find_element_by_xpath('xpath here')

Upvotes: 1

Related Questions