0

I am trying to grab the monthly cost estimate using xpath on selenium but I am having a hard time getting the right value as there are two and possibly more div classes that define property taxes or home insurance. I have my code currently running:

principal_and_interest = (driver.find_element_by_xpath('.//div[@class="sc-Rmtcm GNsVX"]/span[@class="Text-aiai24-0 cJeryq"]').text)

but after running it over different listings, I see that some listings use a different div class name (see pictures to compare). My question is how do I get it so that it either takes into account both (or more div cases) or ,even better, a way that will look for 'principal_and_interest' despite the div classes being different?

div class for other listings

div class for listings that works for me

YuanL
  • 67
  • 8

2 Answers2

0

If you just want to grab the span with 'Principal and interest' text then you can try this

//span[contains(.,'Principal & interest')]

Zmehak
  • 11
  • 4
  • Thanks @Zmehak! Can you help me understand what the period in contains(. means? – YuanL May 12 '20 at 21:35
  • @Yuanl read it here https://stackoverflow.com/questions/3655549/xpath-containstext-some-string-doesnt-work-when-used-with-node-with-more – Zmehak May 12 '20 at 21:39
0

To get the price of Principal & interest try following xpath

print(driver.find_element_by_xpath("//span[text()='Principal & interest']/following-sibling::span").text)

To get list of prices based on Principal & interest

for price in driver.find_element_by_xpath("//span[text()='Principal & interest']/following-sibling::span"):
    print(price.text)
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • That worked very well! Thank you! Can you explain the '/following-sibling::span' portion of the code so I can better understand this? – YuanL May 12 '20 at 21:34
  • @Yual : `'/following-sibling::span` will identify the next span tag of the previous tag. – KunduK May 13 '20 at 08:25