【问题标题】:Selenium finds element but randomly doesn't click an element although it says so on firefoxSelenium 找到元素,但随机不单击元素,尽管它在 Firefox 上这么说
【发布时间】:2017-05-02 04:37:50
【问题描述】:

我正在开发一个程序来自动填写一些表格,并且我正在使用 Selenium。如果这项工作从一开始就一直困扰着我的一个问题是,应该单击按钮的指令不起作用并且不返回任何错误。这是随机发生的,但它曾经在我第一次运行程序时更频繁地发生。

基本上,驱动程序必须进入一个站点,用登录名填充一个弹出窗口,然后当第一页加载时,必须单击一个按钮,这将打开第二个页面。发生的情况是,在没有打开第二页的情况下,驱动程序已经在寻找属于它的元素。

起初我认为浏览器和驱动程序可能不同步,因为驱动程序比浏览器更快,并且以某种方式弄乱了整个过程。我试图用半秒钟的睡眠来纠正这个问题。还实现了 15 秒的隐式等待。

没有任何改进,我决定检查是否找到第一个元素。我插入了一行将按钮上的文本打印到控制台,并且打印正确。下一行是单击该按钮,虽然驱动程序没有检索到错误,但在浏览器上,只加载了第一页。

最后但并非最不重要的一点是尝试使用该 xpath 查找所有元素,但列表只有一个元素。

所以,总而言之,找到第一个元素,点击指令检索没有错误,但仍然没有点击按钮。这是随机发生的。

这是我正在使用的代码的相关部分:

tester.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    tester.get(website);
        Thread.sleep(1000);

        //Bypasses login pop-up window
        try
        {
            bypassLogin(username, password);
        }
        catch(Exception e)
        {
            handler(e.getMessage(), null);
            finisher(tester, br, br2, pathGecko, pathTemplate);
        }

        //Sleep in order to synchronize driver and browser
        Thread.sleep(1000);

        while((reader = br2.readLine()) != null)
        {
            tempOrig = new StringBuilder()
                            .append(tempOrig)
                            .append(reader)
                            .toString();
        }

        //Enters the body frame, with the elements to work with
        try
        {
            tester.switchTo().frame("portal_header");
            List <WebElement> LClick = tester.findElements(By.xpath(".//*[@id='2']/a"));
            System.out.println(LClick.size()); //Returns 1
            tester.findElement(By.xpath(".//*[@id='2']/a")).getText(); //Returns the correct value
            tester.findElement(By.xpath(".//*[@id='2']/a")).click();
            tester.switchTo().defaultContent();
            tester.switchTo().frame("portal_body");
        }
        catch(Exception e)
        {
            handler(e.getMessage(), null);
            finisher(tester, br, br2, pathGecko, pathTemplate);
        }

【问题讨论】:

  • 尝试使用 JavaScript 点击。 IWebElement element = tester.FindElement(By.xpath(".//*[@id='2']/a")); IJavaScriptExecutor js = (IJavaScriptExecutor)tester; js.ExecuteScript("arguments[0].click();", element); 使用新版本的 Firefox 和 Gecko 驱动程序,我在单击按钮时遇到问题。这解决了我的问题。请试一试。代码在 C# 中。
  • 这段代码对我不起作用

标签: java selenium firefox xpath webdriver


【解决方案1】:

试试这个并检查

WebDriverWait wait = new WebDriverWait(tester, 15);
boolean bool = tester.findElement(By.xpath(".//*[@id='2']/a")).isDisplayed();
System.out.print(bool);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='2']/a")));
tester.findElement(By.xpath(".//*[@id='2']/a")).click();

同时检查浏览器版本和 selenium 版本

【讨论】:

  • 这段代码对我不起作用
【解决方案2】:
        IWebElement element = driver.FindElement(GridElementBy(gridId, rowIndex, colIndex));
        Scroll(element);
        IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
        js.ExecuteScript("arguments[0].click();", element);

使用javascript点击也不起作用

【讨论】:

    猜你喜欢
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2021-12-26
    • 2019-04-16
    • 1970-01-01
    • 2019-11-02
    • 2019-04-26
    相关资源
    最近更新 更多