m1ck3y
m1ck3y

Reputation: 13

How to check if a text exists on a web page(not the source) using python and selenium?

I used driver.find_element_by_id('id').is_displayed() to search for certain id in page. It returns true if it is present in page source. I want it to return true only if it is displayed on webpage.

Upvotes: 1

Views: 985

Answers (2)

Kyle Hurst
Kyle Hurst

Reputation: 252

There are only a few HTML ways to hide something from the screen, you could look for those as part of your element style.

    style="display:none; font-size:1px; line-height:1px; max-height:0px; max-width:0px; opacity:0; overflow:hidden; mso-hide:all;"

Those are the ones I usually find when I'm trying to either hide triggers on my webpage or find triggers that others have left on their page. You're just doing the opposite of that.

I typically will use lxml.etree to do some xpath searches to make this easier. If you're working with pages that have active JS though, you might want to consider using something like Selenium that will trigger the JS since that may populate content after landing on the page that requests/lxml won't capture.

Upvotes: 0

Sameer Arora
Sameer Arora

Reputation: 4507

You can check it by fetching the list of that webelement and checking if the size is greater than 0 or not. If the size is greater than 0 then the element is displayed on the page otherwise it is not.
You can do it like:

if(len(driver.find_elements_by_id('id'))>0):
    print("Element is displayed")
else:
    print("Element is not displayed")

Upvotes: 2

Related Questions