org.openqa.selenium.NoSuchElementException,通常称为NoSuchElementException,扩展了org.openqa.selenium.NotFoundException,它是WebDriverException的一种类型。
NoSuchElementException可能在以下两种情况下被抛出:
When using WebDriver.findElement(By by)
:
//example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
When using WebElement.findElement(By by)
:
//example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
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 的方法可以是以下任意一种:
Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.
Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?
Use executeScript()
method to scroll the element in to view as follows :
WebElement elem = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium
Incase element is having the attribute style="display: none;", remove the attribute through executeScript()
method as follows :
WebElement element = driver.findElement(By.xpath("element_xpath"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element)
element.sendKeys("text_to_send");
To check if the element is within an <iframe>
traverse up the HTML to locate the respective <iframe>
tag and switchTo()
the desired iframe through either of the following methods :
driver.switchTo().frame("frame_name");
driver.switchTo().frame("frame_id");
driver.switchTo().frame(1); // 1 represents frame index
Here you can find a detailed discussion on Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?.
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with ExpectedConditions set to proper method as follows :
To wait for presenceOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
To wait for visibilityOfElementLocated :
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
To wait for elementToBeClickable :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
你的代码是正确的,我怀疑页面没有完全加载时找到元素可能是导致问题的原因。
在查找元素之前尝试添加一个较长的等待时间,如果添加等待时间有效,则将其更改为“等待”。
这里是代码,它的意思是如果元素不存在,则等待10秒:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "originTextField"))
)
sleep(1000);
然后输入 driver.findElement(By.id("originTextField"))
。 - RochaaPwait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("originTextField")));
或者
wait = new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.visibilityOf(element)).wait(20);
executeScript()
方法在Python中的等效方法?非常感谢! - Solal