如何解决Selenium无法进行,因为Chrome无法加载UI
有时候,Selenium C#会启动chrome和网站,但无法与之交互。
仔细检查后,我发现这是因为chrome不在焦点上,并且没有加载UI。
我将chrome对准焦点后,会看到白色屏幕半秒钟,然后出现UI。界面一出现,测试就开始运行良好。
我觉得没有背景,Chrome无法获得足够的系统资源。任何帮助,将不胜感激。我从来没有在selenium-python中遇到过这个问题。
网站是-web.whatsapp.com
解决方法
您似乎没有给页面加载足够的时间。您可以使用以下任何一种等待技术。
在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/
,请尝试以下操作-它会强制将重点放在Chrome上(根据提供的信息,我可以假设存在一个JS事件,该事件会阻止页面加载或触发onfocus事件)。
var driver = new ChromeDriver();
driver.Manage().Window.Maximize(); // will focus or at least bring to front
// other things to try - switch
driver.SwitchTo().DefaultContent();
driver.SwitchTo().ActiveElement();
// other things to try - JavaScript
((IJavaScriptExecutor)driver).ExecuteScript("document.querySelector('body').focus();")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。