繁体   English   中英

Selenium 无法继续,因为 chrome 不加载 UI

[英]Selenium can not proceed as chrome Does not loads UI

有时,Selenium C# 会启动 chrome 和网站,但无法与之交互。

仔细检查后,我注意到这是因为 chrome 没有聚焦并且没有加载 UI。

当我聚焦 chrome 时,我会看到半秒钟的白屏,然后出现 UI。 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(根据提供的信息,我可以假设存在阻止页面加载或触发 onfocus 事件的 JS 事件)。

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();")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM