【问题标题】:python selenium find a button through relative xpathpython selenium通过相对xpath找到一个按钮
【发布时间】:2021-01-08 14:15:12
【问题描述】:

我想获取卖家的联系方式(电子邮件和电话号码)。

[<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>]

以下操作无效:

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')

当拍卖链接更改时,从 Chrome 或 CSS 选择器复制的 Xpath 是否停止工作? 将如何确定相对的 Xpath 或者如何单击这三个按钮查看卖方的数据?

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

【问题讨论】:

    标签: python selenium web-scraping xpath google-colaboratory


    【解决方案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)
    

    您可以使用按钮作为参考

    如果出现计时问题,您可以使用显式等待

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

    【讨论】:

    • 点击按钮不起作用,代码:click_on_button = wd.find_element_by_xpath("//*[@data-box-name='SellerEmailShow']/..") click_on_button.click() 返回:pjp*******@****** (pokaż) +48 *** *** *** (pokaż) +48 *** *** *** (pokaż)
    • @dominik driver.find_element_by_xpath("//*[@data-box-name=\"SellerEmailShow\"]").click() ,你必须使用这个来点击然后使用给出的代码
    • 你的表达式没有找到匹配的元素,所以我改成:click_on_button = wd.find_element_by_xpath("//*[@data-box-name='SellerEmailShow']/..") click_on_button.click() print(click_on_button.text)console:pjp*******@****** (pokaż)怎么处理?
    【解决方案2】:

    要打印您需要为visibility_of_element_located() 诱导WebDriverWait 的文本,您可以使用以下基于Locator Strategies

    • 打印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)
      
    • 打印+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)
      

    更新

    要打印完整的电子邮件电话,即pjpauli101@aol.de+48 501 433 898,您可以使用关注 基于Locator Strategies

    • 代码块:

      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)
      
    • 控制台输出:

      pjpauli101@aol.de
      +48 501 433 898
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多