fuhrerguxez fuhrerguxez - 10 days ago 7
Python Question

Python xpath - Exclude content with style="display:none"

I'm using xpath to get some information from a website and I came across a block of code that contains

style="display:none
or
block
and I want to only include the code that has
display:block
; I watched some examples but I couldn't get it working on my code. I want to use an
if
statement to run the code if it has
display:block
but I don't know if that is possible. This is what I have:

if guide_page.xpath(".//div[@class='build-box']/@style/text()") == "display: block;":
for build_names in guide_page.xpath(".//div[@class='build-gradient']"):
for title in build_names.xpath("div/h2/text()"):
print("\n")
print(title)


And this is the
div
that has it:

<div class="build-box" style="display: block;">


I'm not sure if I should paste more of the html or if that's enough, otherwise, please tell me and thanks for any help :)

Answer

You can do this without using if statement. Just add a not(...condition...) in predicate to exclude elements matching certain condition. For example, the following XPath returns div elements with certain class attribute value, that don't have attribute style="display: block;" :

.//div[@class='build-box' and not(@style='display: block;')]