【问题标题】:Not able to locate an element using By.xpath无法使用 By.xpath 定位元素
【发布时间】:2021-08-19 12:53:23
【问题描述】:

我在使用 selenium webdriver 通过 xpath 定位元素时遇到问题。我不是网络开发人员或测试人员。在我的日常任务中出现了一些问题,需要通过脚本连接到网站作为自动化的一部分。所以有人可以帮我解决这个问题。

鉴于下面的 xml,我想自动填充 UsernamePassword

<div class="fd-form-group">
    <div class="fd-form-item">
        <label class="fd-form-label">Username</label>
        <input autocomplete="username" class="fd-input" type="text">
        </div>
    </div>


<div class="fd-form-group">
    <div class="fd-form-item">
        <label class="fd-form-label">Password</label>
        <input autocomplete="current-password" class="fd-input" type="password">
        </div>
    </div>

作为第一步,我尝试使用以下代码定位这些元素

System.setProperty("webdriver.gecko.driver", "/some/path/geckodriver")

  val driver = new FirefoxDriver()
  val myWait = new WebDriverWait(driver, 30)
  val myWebsite = "https://my/website"

  try {
    driver.get(myWebsite)

    // locate username
    myWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@type='text']")))

  } finally driver.quit

我收到此错误:Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.xpath: //input[@type='text']

我也试过@class,但没有帮助我。我知道这个问题是由许多其他用户提出的,并用各种方法解决。但是没有人在这里帮助我,因为我对这些事情不太了解。如果有人能提供帮助,我真的很感激

【问题讨论】:

    标签: java selenium selenium-webdriver xpath css-selectors


    【解决方案1】:

    根据您问题中的详细信息,我猜那里有一个iframe,因此您必须先切换到iframe,然后再尝试访问这些元素。像这样:

    driver.switchTo().frame(driver.findElement(By.id("frameId")));
    myWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@type='text']")))
    

    最后,当您完成 iframe 后,返回默认内容

    driver.switchTo().defaultContent()
    

    【讨论】:

    • 我已经删除了这个 driver.switchTo().defaultContent() 因为它不是必需的。我已经更新了我的问题
    • 我明白了。我不认为这很重要。您试图等待元素存在。如果您在页面上看到该元素但 selenium 无法找到它的存在,我只能考虑 iframe。否则该元素不是该页面,或者您使用了错误的定位器。但是您的定位器与您提供的 HTML 相匹配...
    【解决方案2】:

    您需要定位输入字段而不是标签。

    用户名 xpath

    //label[text()='Username']/following-sibling::input
    

    密码xpath:

    //label[text()='Password']/following-sibling::input
    

    并像下面这样使用它:

    myWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//label[text()='Username']/following-sibling::input"))).sendKeys("Your username")
    

    我不确定你为什么使用 driver.switchTo().defaultContent() 它在 iframe 中吗?还是在新窗口中?

    【讨论】:

    • 谢谢@cruisepandey。我试过你的建议,但我得到了同样的错误。我已经删除了这个driver.switchTo().defaultContent() ,因为它不是必需的
    • 这是错误Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: //label[text()='Username']/following-sibling::input
    • 试试这个 xpath 的用户名://input[@autocomplete='username' and @type='text']
    • 是的.. 我刚试过。同样的错误:(。问题是我没有得到任何详细的日志。我很难调试它
    • 我认为它在 iframe 中,做一件事。按 f12 并转到元素并搜索 //iframe,如果您看到任何黄色条目,请告诉我
    猜你喜欢
    • 2017-11-30
    • 2023-04-01
    • 2021-01-03
    • 2021-02-11
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多