【问题标题】:Selenium Chrome driver/Java - Can't find element (text field) by IdSelenium Chrome 驱动程序/Java - 无法按 Id 找到元素(文本字段)
【发布时间】:2021-08-17 03:26:28
【问题描述】:

我有一个简单的 HTML 网页,其中包含嵌套在一些 div 和 </form> 中的文本字段(我看不到 IFrame)。我确定我使用了正确的 ID,但是在 IntelliJ 中运行代码时它一直失败。这是元素的html。这是我得到的例外:

异常

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="loginLastName"]"}

以及我试图通过 selenium 定位的元素(文本字段)的 HTML:

HTML:

<input type="text" maxlength="32" minlength="2" required="" id="loginLastName" class="grv-input " aria-describedby="loginLastNameAria" pattern="(([ \-.']*)?[A-Za-z]+([ \-.']*)?)*">

在 java 代码中,我的浏览器正确加载到像 http://google.com 这样的虚拟站点,但未能从我的相关站点中获取 elementId。我尝试使用 ID 和 XPATH(我从 chrome 浏览器获得):

Java

// get by ID
driver.findElement(By.id("loginLastName")).sendKeys("This is a test");

// get by XPATH
driver.findElement(By.xpath("//*[@id=\"loginLastName\"]")).sendKeys("dsdfdsfdsdsdsfdsgsdfgsdfdsg");

我还尝试添加 延迟 以确保组件有时间加载,但没有任何变化

WebDriverWait wait = new WebDriverWait(driver, 100);
element = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginLastName")));

嵌套在两个&lt;divs&gt; 和一个&lt;form&gt; 中是否重要?我是否必须遍历这些元素,才能深入了解这一点或其他内容? (似乎过于复杂,虽然我没有用硒做那么多)。

告诉我!

【问题讨论】:

  • 你能分享页面的完整 HTML 或链接吗?嵌套的 div 和表单应该无关紧要。
  • @C.Peck 这是此页面的一个变体:capitalone.com/cars/login 但是,我刚刚链接你的那个有效。我想知道这是否与为我发布的网页(旧版)定义 HTML 的方式有关。
  • 即使我从谷歌浏览器复制“完整xpath”并在Java代码中使用它来通过xpath查找,它仍然不起作用:org.openqa.selenium.NoSuchElementException:没有这样的元素: 无法定位元素:{"method":"xpath","selector":"/html/body/div/div/div/div/div/div[2]/div/refi-common-login-form/ /form/refi-common-last-name//div/input"}(会话信息:chrome=91.0.4472.77)
  • 这就是从完整的 xpath 中出来的?完整的 xpath 中不应有双斜杠 //。无论如何,如果没有真正看到您实际使用的页面的整个 HTML,这可能是无法诊断的。
  • 您在评论中发布的页面上没有 id loginLastName

标签: java html selenium selenium-chromedriver ui-automation


【解决方案1】:

好的,看到您的页面后,我相信我知道问题所在。您尝试识别的“姓氏”字段位于两个嵌套的shadow-root 元素中。 Selenium 不会查看影子根,除非您识别它并告诉它查看那里。尝试使用这样的代码:

JavascriptExecutor js;
WebElement shadowRootElement = js.executeScript('''return document.querySelector("refi-common-login-form").shadowRoot''');

WebElement shadowRootChild = js.executeScript('''return arguments[0].querySelector("refi-common-last-name").shadowRoot''', shadowRootElement);

shadowRootChild.findElement(By.id("loginLastName")).sendKeys("This is a test");

【讨论】:

  • 我试过了,但无法开始工作。我还尝试了一些非常基本的东西,比如尝试通过标签名称简单地获取 并且即使失败(及其在阴影元素之外),即 WebElement root1 = driver.findElement(By.tagName( “refi-common-login-form”)); ...所以现在在我看来,我什至无法使用 selenium 定位器从中获取任何元素...这里发生了什么??
  • 当你说你无法让它工作时,到底出了什么问题?你有什么错误吗?
  • 我尝试通过 tagName(应该是最简单的选项)、xpath 和 cssselector 抓取它(作为第一个阴影元素的“宿主”的普通 DOM 元素);都没有工作。异常消息是:org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"refi\-common\-login\-form"}
  • 不要用普通的 selenium 函数来识别它;你试过使用WebElement shadowRootElement = js.executeScript('''return document.querySelector("refi-common-login-form").shadowRoot''');
  • 我得到了异常:org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'shadowRoot' of null 但我必须远程引用你的三引号,因为这会引发 Java 编译器 (IntelliJ) 错误并像下面这样:WebElement shadowRootElement = (WebElement) js.executeScript("return document.querySelector(\"refi-common-login-form\").shadowRoot");
【解决方案2】:

你能试试下面的Xpaths吗

#Get Xpath with combination
//input[@type='text' and @id='loginLastName']

#Xpath assuming there are multiple elements (DOM  mentioning 1 of 2 elements)
(//input[@type='text' and @id='loginLastName'])[1]

#Get Xpath with another combination
//input[@type='text' and @aria-describedby='loginLastNameAria']

附录

List <WebElement> elementCheck = driver.findElements(By.xpath("<xpath of element>"))

if (elementCheck.size() == 0){

//Element not present when entered to the page
//Do something

}else {

//Do something

}

【讨论】:

猜你喜欢
  • 2018-08-15
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
相关资源
最近更新 更多