【问题标题】:Selenium Webdriver find element by Xpath problem with String quotation marksSelenium Webdriver通过带有字符串引号的Xpath问题查找元素
【发布时间】:2025-12-29 07:05:12
【问题描述】:

这是我的代码:

var loginButton = driver.FindElement(By.XPath("//*[@id="login - view"]/form/div[3]/button"));

我已经看到这个帖子了:Find texts that contains quotation marks by xpath in Selenium

但这对我没有帮助

引号是问题

【问题讨论】:

  • 你有完整的html吗?然后使用 div[3] 可能有更好的 xpath。它们非常脆弱。
  • 是的,这里是按钮<button type="submit" aria-label="Login button" class="btn btn-large p-x-2 btn-inverse" disabled="">Log In</button>

标签: c# string selenium selenium-webdriver quotation-marks


【解决方案1】:

尝试使用单引号将 id 值放入字符串中。

var loginButton = driver.FindElement(By.XPath("//*[@id='login - view']/form/div[3]/button"));

【讨论】:

    【解决方案2】:

    id 属性值是否正确? “-”前后有一些空格

    【讨论】:

    • 是的,我直接复制了 Xpath
    【解决方案3】:

    根据您在此处添加的内容:

    <button type="submit" aria-label="Login button" class="btn btn-large p-x-2 btn-inverse" disabled="">Log In</button>
    

    这个 disabled 属性会导致问题,因为它使元素,你猜对了,被禁用了!见-https://www.w3schools.com/tags/att_disabled.asp

    如果你删除它,它看起来像这样:

    <button type="submit" aria-label="Login button" class="btn btn-large p-x-2 btn-inverse">Log In</button>
    

    那么你可以使用下面的,它会正常工作的。

    //button[text()='Log In']
    

    是否还有其他未禁用的登录按钮?

    【讨论】:

    • 我知道按钮被禁用,但这不是问题,我的 XPath 字符串有问题
    • 尝试包含。 //*[包含(文本(),'登录 - 视图')]
    • 我已经尝试过了,但这会改变 xpath,然后他不再有效