0

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
dominik
  • 61
  • 1
  • 2
  • 8

2 Answers2

1
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\"]/.."))
)
PDHide
  • 18,113
  • 2
  • 31
  • 46
  • click on the button doesn't work, the code: `click_on_button = wd.find_element_by_xpath("//*[@data-box-name='SellerEmailShow']/..") click_on_button.click()` return: `pjp*******@****** (pokaż) +48 *** *** *** (pokaż) +48 *** *** *** (pokaż)` – dominik Jan 08 '21 at 17:56
  • @dominik driver.find_element_by_xpath("//*[@data-box-name=\"SellerEmailShow\"]").click() , you have to use this to click and then use the code given – PDHide Jan 08 '21 at 18:06
  • your expression doesn't find the matching element, so I changing it to: `click_on_button = wd.find_element_by_xpath("//*[@data-box-name='SellerEmailShow']/..") click_on_button.click() print(click_on_button.text)` console: `pjp*******@****** (pokaż)` How to deal with it? – dominik Jan 09 '21 at 10:31
0

To print the texts you need to induce WebDriverWait for the visibility_of_element_located() you can use the following based Locator Strategies:

  • Printing pjp*******@****** (pokaż):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(., 'pytanie do sprzedającego')]//following::li[1]/div"))).text)
    
  • Printing +48 *** *** *** (pokaż):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(., 'pytanie do sprzedającego')]//following::li[2]/div"))).text)
    

Update

To print the full email and phone i.e. pjpauli101@aol.de and +48 501 433 898 you can use the following based Locator Strategies:

  • Code Block:

    driver.get("https://allegro.pl/oferta/faller-170601-plyta-dekoracyjna-droga-brukowana-h0-9303217744#aboutSeller")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-role='accept-consent']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-box-name='SellerEmailShow']"))).click()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@data-box-name='SellerEmailClick']"))).text)
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@data-box-name='SellerMobileShow']"))).click()
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//li[.//a[@data-box-name='SellerEmailClick']]//following::li[1]/div"))).text)
    
  • Console Output:

    pjpauli101@aol.de
    +48 501 433 898
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352