fuhrerguxez fuhrerguxez - 9 months ago 63
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

and I want to only include the code that has
; I watched some examples but I couldn't get it working on my code. I want to use an
statement to run the code if it has
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()"):

And this is the
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 Source

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;')]