【问题标题】:Selenium can not proceed as chrome Does not loads UISelenium 无法继续,因为 chrome 不加载 UI
【发布时间】:2020-12-09 10:12:27
【问题描述】:

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

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

当我聚焦 chrome 时,我会看到半秒钟的白屏,然后出现 UI。 UI一出现,测试就开始运行良好。

感觉chrome在没有后台的情况下无法获得足够的系统资源。任何帮助,将不胜感激。我从未在 selenium-python 中遇到过这个问题。

网站是 - web.whatsapp.com

【问题讨论】:

  • 它应该做什么?它在做什么?

标签: c# selenium google-chrome selenium-chromedriver


【解决方案1】:

您似乎没有给页面加载足够的时间。您可以使用以下任何一种等待技术。

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/

【讨论】:

  • 对不起,但至少你应该在 cmets 中询问。我已经在使用显式等待了。
  • 请说对不起,伙计。没关系,因为你没有在你的问题中提到,我在这里放了一些信息。另外,如果 UI 加载是一个问题,我不确定您是否尝试过无头浏览器。干杯:)
  • 无头也是一个问题。一个月以来我不能运行无头铬或铬。我想知道为什么它不是一个普遍的问题。至少 chrome-dev 应该让问题更开放,让我们可以在 github 上打开问题。
  • 我完全同意你的看法。任何方式我都会尝试使用 python 脚本并让你知道我是否能够复制和解决问题。感谢您的突出显示!!!
【解决方案2】:

请尝试以下操作 - 它会强制将焦点放在 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();")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2016-05-02
    • 2014-01-04
    相关资源
    最近更新 更多