您似乎没有给页面加载足够的时间。您可以使用以下任何一种等待技术。
Selenium C# 中的显式等待
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
String ele_xpath = "<xpath of element>"
WebDriverWait wait = new WebDriverWait(driver,30);
IWebElement welcomeMessage =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.XPath(ele_xpath)));
// Here I have assumed first page is having a welcome message, you can use any element present on your page. Your script will wait up to 30 sec for element to appear before it will throw timeoutexception
Selenium C# 中的隐式等待
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//Above will make script to wait for each element up to 30 sec
在 C# 中等待:
您可以让脚本在执行下一行代码之前等待定义的毫秒数:
Thread.Sleep(6000);
# It will for 6 sec
在 Selenium C# 中流畅等待
Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
.withTimeout(30, SECONDS) // this defines the total amount of time to wait for
.pollingEvery(2, SECONDS) // this defines the polling frequency
.ignoring(NoSuchElementException.class); // this defines the exception to ignore
WebElement welcomeMessage= fluentWait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) //in this method defined your own subjected conditions for which we need to wait for
{ return driver.findElement(By.xpath("//*[contains(text(),'Welcome')]"));
}});
注意:您可以使用上述任何一种等待方法。然而,他们每个人都有自己的优势/劣势。请在下面的链接中阅读更多关于它们和区别的信息:
https://www.lambdatest.com/blog/selenium-waits-implicit-explicit-fluent-and-sleep/