【问题标题】:unique xpath for element span inside div inside button selenium python按钮 selenium python 内 div 内元素跨度的唯一 xpath
【发布时间】:2021-09-26 21:33:03
【问题描述】:

Xpath

我正在寻找此元素的唯一 Xpath(选择) 在谷歌语言设置中 https://myaccount.google.com/language?hl=en 已经有了这个 xpath,但我需要更准确和独特的东西

    WebDriverWait(driver, 30).until(
        EC.element_to_be_clickable((By.XPATH, 
            "//*[@id='yDmH0d']/div[11]/div/div[2]/div[3]/div[2]/button"))).click()

【问题讨论】:

  • 此 xpath 位于浏览器上的 de 按钮,让我知道它是否适合您://div[child::div[.="Add Language"]]/descendant::button[@aria-label="Select your language"]
  • 错误是什么?您在点击按钮之前选择了语言吗?

标签: python selenium xpath


【解决方案1】:

如果你注意 HTMLDOM :

您只能基于属性名称构造xpath。你不需要他们的values 也不需要their text

//div[@data-is-touch-wrapper]/button[@data-id and @aria-label]

表示两个匹配节点。

(//div[@data-is-touch-wrapper]/button[@data-id and @aria-label])[2]

应该完成工作。

【讨论】:

  • WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "(//div[@data-is-touch-wrapper]/button[@data-id and @aria-label])[2]"))).click() 这个可以正常工作吗?
  • 是的。 xpath 索引没有害处。当您执行此操作并遇到任何问题时,请告诉我。
  • 没问题,但因为 "(//div[@data-is-touch-wrapper]/button[@data-id and @aria-label])[2]" 是这样的权利在带有 () 的代码中
  • 是的,当然是。
【解决方案2】:

我会尝试从浏览器中复制 X-Path,如下所述: Mozilla Help

我的结果是://*[@id="lang-selector"]

【讨论】:

    猜你喜欢
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 2020-04-26
    相关资源
    最近更新 更多