Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

python selenium find a button through relative xpath

I would like to get the contact details (email and phone numbers) of the seller.

[<ul class="_17qy1 _1rj80 _1sql3 _3a4zn"><li><a class="_w7z6o" data-box-name="AskSellerClick" href="#zadaj-pytanie" rel="nofollow">pytanie do sprzedaj?cego</a></li><li><div>pjp*******@******<!-- --> (<button class="_w7z6o _ypulx" data-box-name="SellerEmailShow" type="button">poka?</button>)</div></li></ul>, <ul class="_17qy1 _1rj80 _1sql3 _3a4zn"><li><div>+48 *** *** ***<!-- --> (<button class="_w7z6o _ypulx" data-box-name="SellerMobileShow" type="button">poka?</button>)</div></li></ul>]

The following did not work:

click_on_button = wd.find_elements(By.CLASS_NAME, 'SellerMobileShow')

click_on_button = wd.find_elements_by_css_selector('body > div.main-wrapper > div:nth-child(3) > div > div > div:nth-child(10) > div > div > div > div > div:nth-child(5) > div:nth-child(2) > div > div > div._t2hyt._l7nkx._r6475._1rcax._nyhhx._1bo4a._r8zxu._1ar9d._62fe8_pdZjg._62fe8_CY37T._62fe8_24sRD > div > div._9f0v0._1xzdi._ai5yc._5d6n2._1h7wt._62fe8_1ibzI > section > div > div > div > div:nth-child(2) > div > div > div._1bo4a._xu6h2._m7qxj._1q55c > section:nth-child(2) > div > div > div:nth-child(2) > ul > li:nth-child(1) > div > button')

find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div[10]/div/div/div/div/div[5]/div[2]/div/div/div[2]/div/div[2]/section/div/div/div/div[2]/div/div/div[2]/section[2]/div/div/div[1]/ul/li[2]/div/button')

When the auction link changes, does the Xpath copied from Chrome or CSS Selector stop working? How will the relarive Xpath be determined or how else can you click on these three buttons to see the seller's data?

https://allegro.pl/oferta/faller-170601-plyta-dekoracyjna-droga-brukowana-h0-9303217744#aboutSeller

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
email = driver.find_element_by_xpath("//*[@data-box-name="SellerEmailShow"]/..")
print(email.text)
phone = driver.find_element_by_xpath("//*[@data-box-name="SellerMobileShow"]/..")
print(phone.text)

You can use button as reference

you can use explicit wait if timing issues occurs

email = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located(
        (By.XPATH, "//*[@data-box-name="SellerEmailShow"]/.."))
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...