a.programmer
a.programmer

Reputation: 57

Checking If Attribute Exists In Any Of Html Tags Selenium Python

I want to check out if any html tags have <style> attribute like <a style = ..> or <h1 style = ...> or <div style = ..> etc. I used below code but it could not be run:

driver = webdriver.Chrome(web_driver_address, options=op)
driver.get(url)

elems = driver.find_elements_by_xpath("[@style]")

How can i fix this?

Upvotes: 0

Views: 998

Answers (2)

Guy
Guy

Reputation: 50809

xpath needs tag to be valid. If you don't want a specific tag use *

find_elements_by_xpath("//*[@style]")

Or with css_selector

find_elements_by_css_selector("[style]")

Upvotes: 1

Prophet
Prophet

Reputation: 33351

Your XPath is missing element tag name.
In your case it can be any tag name, but it still should be there as a part of syntax, so you should use * like any there.
Also, you are missing the // that means the element can be anywhere on the page.
So the correct XPath expression will be something like this:

elems = driver.find_elements_by_xpath("//*[@style]")

Don't forget to add some wait / delay to let page load all the elements before you get them

Upvotes: 1

Related Questions