我想在 selenium 中找到我的 TextField,但我不知道如何(我第一次使用 Selenium)。
我尝试过:
driver.findElement(By.id("originTextField"))
或者通过开发工具中chrome生成的xPath和cssSelector字符串。
请帮助我,我将不胜感激。
这是 html:
org.openqa.selenium.NoSuchElementException,俗称 NoSuchElementException 扩展了 org.openqa.selenium.NotFoundException,它是 WebDriverException 的一种类型。
NoSuchElementException 可以在以下 2 种情况下抛出:
使用时
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的原因可能是以下任一原因:
<iframe>
标签内。
NoSuchElementException 的解决方案可以是以下任意一种:
定位器策略,它唯一标识所需的WebElement。您可以借助开发人员工具(Ctrl+Shift+I或F12)并使用元素检查器。
在这里您将找到关于WebElement elem = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
在这里您将找到关于 的详细讨论
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
在这里您可以找到关于
Selenium Webdriver Java 中不使用 driver.switchTo().frame(“frameName”) 是否可以切换到框架中的元素?.
的详细讨论HTML DOM 中present/visible,则将 ExpectedConditions 设置为正确的方法来引发 WebDriverWait,如下所示:
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
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']")));
Selenium的python基于客户端的相关讨论:
尝试在查找元素之前添加长时间睡眠,如果添加睡眠有效,请将睡眠更改为等待。
这是代码,这意味着如果元素不存在则等待10秒:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "originTextField"))
)
谢谢你的建议!!
wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("originTextField")));
或
wait = new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.visibilityOf(element)).wait(20);