我想在 selenium 中找到我的 TextField,但我不知道如何(我第一次使用 Selenium)。
我尝试过:
driver.findElement(By.id("originTextField"))
或者通过chrome在开发工具中生成的xPath和cssSelector字符串。
请帮助我,我将不胜感激。
这是 html:
请您参考如下方法:
NoSuchElementException
org.openqa.selenium.NoSuchElementException通常称为 NoSuchElementException 扩展 org.openqa.selenium.NotFoundException这是 WebDriverException 的一种类型.
NoSuchElementException 可以在以下两种情况下抛出:
使用
WebDriver.findElement(By by)
时://example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
使用
WebElement.findElement(By by)
时://example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
根据 JavaDocs,就像任何其他 WebDriverException 一样,NoSuchElementException 应包含以下常量字段:
Constant Field Type Value
SESSION_ID public static final java.lang.String "Session ID"
e.g. (Session info: chrome=63.0.3239.108)
DRIVER_INFO public static final java.lang.String "Driver info"
e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)
BASE_SUPPORT_URL protected static final java.lang.String "http://seleniumhq.org/exceptions/"
e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)
<小时 />
原因
NoSuchElementException 的原因可能是以下任一原因:
- 您采用的定位器策略未识别 HTML DOM 中的任何元素.
- 您采用的定位器策略无法识别该元素,因为它不在浏览器的 Viewport 内.
- 您采用的定位器策略标识了该元素,但由于属性style="display: none;"的存在而不可见。
- 您采用的定位器策略无法唯一标识HTML DOM中所需的元素,并且当前会找到一些其他隐藏/不可见元素。
- 您尝试查找的WebElement位于
<iframe>
内标签。 - 即使在 HTML DOM 中出现/可见该元素之前,WebDriver 实例也会寻找 WebElement。
解决方案
解决NoSuchElementException的解决方案可以是以下任一:
采用Locator Strategy它唯一标识所需的WebElement。您可以借助开发人员工具(Ctrl+Shift+I 或 F12)并使用元素检查器。
在这里您可以找到关于 how to inspect element in selenium3.6 as firebug is not an option any more for FF 56? 的详细讨论
使用
executeScript()
滚动元素查看的方法如下:WebElement elem = driver.findElement(By.xpath("element_xpath")); ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
在这里您可以找到关于 Scrolling to top of the page in Python using Selenium 的详细讨论
如果元素具有属性style="display: none;",请通过
executeScript()
删除该属性方法如下:WebElement element = driver.findElement(By.xpath("element_xpath")); ((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element) element.sendKeys("text_to_send");
检查元素是否在
<iframe>
内向上遍历 HTML 以找到相应的<iframe>
标签和switchTo()
通过以下任一方法获取所需的iframe:driver.switchTo().frame("frame_name"); driver.switchTo().frame("frame_id"); driver.switchTo().frame(1); // 1 represents frame index
在这里您可以找到关于 Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java? 的详细讨论.
如果该元素未立即在 HTML DOM 中存在/可见,则引发 WebDriverWait与 ExpectedConditions设置为正确的方法如下:
-
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
等待visibilityOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
-
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
-