1

I am scraping images from a website, but the xpath varies for the element. I read this post that an or function can be used by doing find_elements_by_xpath.

However, when I tried replicating the example, I could no longer extract the src of the image. Am I doing the or function wrong?

images=driver.find_elements_by_xpath('// img[ @ id = "ember51"]' or '// img[@id="ember52"]')
print(images.get_attribute('src'))

Error:

AttributeError: 'list' object has no attribute 'get_attribute'

I attempted to make a for loop, so the src could be scraped:

images=driver.find_elements_by_xpath('// img[ @ id = "ember51"]' or '// img[@id="ember52"]')
for image in images:
    print(images.get_attribute('src'))

But when I use a for loop, I get the same error. Not sure how to make this work.

Yus Ra
  • 97
  • 1
  • 6

2 Answers2

0

Use the | as or operator.

images=driver.find_elements_by_xpath('//img[@id = "ember51"]|//img[@id="ember52"]')

enter image description here

Make sure the entire xpath is wrapped in '.

Community
  • 1
  • 1
supputuri
  • 13,644
  • 2
  • 21
  • 39
0

You have invalid xpath expresion:

// img[ @ id = "ember51"]' or '// img[@id="ember52"]

It's should:

//img[@id="ember51" or @id="ember52"]

Try this:

images=driver.find_elements_by_xpath('//img[@id="ember51" or @id="ember52"]')
for image in images:
    print(images.get_attribute('src'))
frianH
  • 7,295
  • 6
  • 20
  • 45