【问题标题】:Webelement.Click() - Getting exception on Webelement.Click() though it clicks the ElementWebelement.Click() - 在 Webelement.Click() 上获得异常,尽管它点击了元素
【发布时间】:2016-10-12 15:46:04
【问题描述】:

操作系统:Windows 硒版本:2.53.1.0 IDE:Visual Studio 2013 浏览器:Internet Explorer 11 版本 11.420

当我尝试单击网页上的元素时出现异常。当单击链接并打开一个对话框时会发生这种情况。 Webelement.click() 函数单击元素并打开模式对话框,但 Click() 需要时间返回并最终将异常记录为“对远程 WebDriver 服务器的 URL 的 HTTP 请求“”在 60 秒后超时。 '

预期行为:

应该点击“Firefox Beta”下载按钮,会出现带有运行和保存选项的“IE工具栏”

实际行为:

点击“Firefox Beta”下载按钮,“IE工具栏”就来了。 但 downloadElement.Click() 等待 60 秒并抛出异常

重现步骤:

下面是sn-p代码:

string url = "https://www.mozilla.org/en-US/firefox/channel/#beta";
try{
   IWebDriver driver = new InternetExplorerDriver();        
   driver.Navigate().GoToUrl(url);    
   Thread.Sleep(5000);    
   IWebElement downloadElement = driver.FindElement(By.XPath("//div[@id='download-button-desktop-beta']/ul/li/a/strong"));    
   Thread.Sleep(5000);    
   downloadElement.Click();   
  }catch{
     //catch block
  }

【问题讨论】:

    标签: selenium selenium-webdriver click selenium-iedriver


    【解决方案1】:

    尝试使用这个 xpath 而不是那个。

    IWebElement downloadElement = driver.FindElement(By.XPath("/html/body/div[2]/div/main/section[1]/div/div/ul/li[1]/a/strong"));
    

    IE11 selenium 有时会出现一些问题,无法按预期工作。所以我在某些情况下使用双击而不是单击。

    Actions action = new Actions(driver);
    action.moveToElement(driver.findElement(By.xpath("/html/body/div[2]/div/main/section[1]/div/div/ul/li[1]/a/strong"))).doubleClick().perform();
    

    两个都试试,希望对你有帮助

    【讨论】:

    • 谢谢 Anuraj.. 第二个选项有帮助。 :)
    • @Aniruddhya,总是乐于提供帮助。 :-) 投票给答案,这对其他人也有帮助
    【解决方案2】:

    您可以在 downloadElement.Click() 之后添加隐式等待,并等待模态对话框完全加载。

    【讨论】:

    • 可以添加隐式等待语句,如下图:downloadElement.Click(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    • 或downloadElement.Click(); WebDriverWait 等待 = 新 WebDriverWait(drv,30); wait.until(ExpectedConditions.visibilityOfElementLocated(By.....path of dialod box));或者 wait.until(ExpectedConditions.alertIsPresent()) !=null);
    【解决方案3】:

    试试这个。它对我有用 -

    package sbps;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    
    public class cvs_signup
    {
        public static void main(String[] args) {
    
            String url = "https://www.mozilla.org/en-US/firefox/channel/#beta";
            try{
               WebDriver driver = new InternetExplorerDriver();
               driver.get(url);
               Thread.sleep(5000);
               WebElement downloadElement = driver.findElement(By.xpath("(//a[@href='https://download.mozilla.org/?product=firefox-beta-stub&os=win&lang=en-US'])[last()]"));
               Thread.sleep(5000);
               downloadElement.click();
              }catch(Exception e){
                 //catch block
              }
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 2018-12-25
      • 1970-01-01
      • 2018-11-01
      • 2019-04-29
      • 1970-01-01
      • 2020-08-07
      • 2019-11-06
      • 2018-12-19
      相关资源
      最近更新 更多