Peak Dermutag
Peak Dermutag

Reputation: 103

Find elements by XPath with fallback

for products in self.br.find_elements_by_xpath("//*[@class='image']/a"):
    self.urls.append(products.get_attribute("href"))

This code will find all hrefs links by the class.

My problem that the webpage has a changing source sometimes it can be //*[@class='image']/a but sometimes //*[@class='newPrice']/a. How can I change the for loop to use the other expression if the first xpath option found nothing?

Upvotes: 0

Views: 3184

Answers (2)

paul trmbrth
paul trmbrth

Reputation: 20748

Not equivalent to a fallback, but you could use an OR syntax:

for products in self.br.find_elements_by_xpath(
        "//*[@class='image']/a | //*[@class='newPrice']/a"):
    self.urls.append(products.get_attribute("href"))

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1123710

Store the output in a variable first:

links = self.br.find_elements_by_xpath("//*[@class='image']/a")
if not links:
    links = self.br.find_elements_by_xpath("//*[@class='newPrice']/a")
for products in links:
    self.urls.append(products.get_attribute("href"))

Upvotes: 4

Related Questions