0

So to find a word in a certain page i decided to do this:

    bodies = browser.find_elements_by_xpath('//*[self::span or self::strong or self::b or self::h1 or self::h2 or self::h3 or self::h4 or self::h5 or self::h6 or self::p]')
    for i in bodies:
        bodytext = i.text
        if "description" in bodytext or "Description" in bodytext:
            print("found")
            print(i.text)

I believe the code itself is fine, but ultimately I get nothing. To be honest I am not sure what is happening, it just seems like it doesn't work. Here is an example of what website it would be ran on. Here is some of the page source:

<h2>product description</h2>

EDIT: It may be because the element is not in view, but I have tried to fix it with that in mind. Still, no luck.

2
  • 1
    I´m no expert in Xpath but it looked wrong to me. I tested the Xpath with this website xpathtester.com/xpath against the xml of the website in your question and it outputs error, probably thats why your code doesn´t output anything. Check your Xpath or first just try to get one element, if it works try to add another, until you get all you want. Commented Nov 30, 2017 at 8:42
  • yeah i know it looks strange, but it works when i try: if "the" in bodytext: Commented Nov 30, 2017 at 22:42

2 Answers 2

2

Inspect the element and check if its inside the iframe,

if it does, you need switch to the frame first

driver.switch_to_frame(frame_id)
Sign up to request clarification or add additional context in comments.

Comments

1

You could try waiting for a few ms to ensure the page has rendered and the expected element is visible with:

time.sleep(1)

Alternatively, try using IDs or custom data attributes to target the elements instead of xpath. Something like find_by_css_selector('.my_class') may work better.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.