首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Selenium IE .click()工作不一致

Selenium IE .click()工作不一致
EN

Stack Overflow用户
提问于 2014-08-12 01:48:56
回答 1查看 1.1K关注 0票数 0

我有以下问题。我使用Selenium 2.42.2测试我们公司的intranet站点。因此,我用一个测试套件实现了一个示例Testcase。我用Firefox 31和IE11来测试这个测试用例。所有的测试都很好,但是有时候IE似乎没有点击某些元素。这非常令人困惑,因为有时它正确地工作,有时它不起作用。然而,到目前为止,我尝试了以下解决方案,但没有成功:

  • 检查缩放级别并以所有可能的方式设置它
  • 设置所有可能的等待(显式、隐式、一些奇怪的变体)
  • 使用‘sendkey(\n)’代替.click()

唯一可行的解决方案是双击。但是,如果这个错误没有发生,那么firefox就会出现新的问题。

有没有人知道是什么原因导致了这个问题?

感谢你的帮助。

所附守则:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@RunWith(Parameterized.class)
public class SeleniumTest_WD_used extends AbstractSelenium {

    public SeleniumTest_WD_used(RemoteWebDriver d) {
        driver = d;
    }

    private String baseUrl = "company.intranet.site.com";

    @Override
    @Before
    public void setUp() {
        driver.get(baseUrl);
        driver.manage().timeouts().implicitlyWait(500, TimeUnit.MILLISECONDS);
    }

    @Test
    public void worldClock_autoCity_Test_with_ES() throws InterruptedException {
        Thread.sleep(2000);
        driver.findElement(By.xpath("some XPath")).click();
        Thread.sleep(2000);
        new Select(driver.findElement(By.id("some ID"))).selectByVisibleText("Some Text");
        driver.findElement(By.cssSelector("Some Css_Element")).click();
        driver.findElement(By.xpath("some XPath")).click();
        RemoteWebElement e1 = (RemoteWebElement) driver.findElement(By.xpath("some XPath"));
        Assert.assertEquals("Some Assert", e1.getText());
    }
}

我以以下方式重写IE和FF驱动程序的“findElement”方法(但如果我使用标准方法,也会遇到这个错误):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class FirefoxDriver2_0 extends FirefoxDriver {
private static FirefoxDriver2_0 instance = null;
private long startTime;
private long stopTime;

private FirefoxDriver2_0() {
    super();
}

public static synchronized FirefoxDriver2_0 getInstance() {
    if (instance == null) {
        instance = new FirefoxDriver2_0();
    }
    return instance;
}

@Override
public RemoteWebElement findElement(By by) {
    return elementSearch(by, FirefoxDriver2_0.getInstance());
}

private RemoteWebElement elementSearch(By by, FirefoxDriver driver) {
    startTime = System.currentTimeMillis();
    RemoteWebElement helpingElement = null;
    isElementPresent(by);
    try {
        helpingElement = (RemoteWebElement) super.findElement(by);
    } catch (Exception e) {
        AllTest.updateLogger("[error] method 'elementSearch' incomplete");
        fail("Test not successfull!");
    } finally {
        stopTime = System.currentTimeMillis();
        timeWarning(by.toString());
    }
    return helpingElement;
}

private boolean isElementPresent(By by) {
    try {
        super.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        stopTime = System.currentTimeMillis();
        timeWarning(by.toString());
        AllTest.updateLogger("[main] ERROR\tThe following expression could not be solved: " + by);
        fail("Test not successfull! --> Error: Element not found. Please check the failed XPath's");
        return false;
    }
}

private void timeWarning(String s){
    if(stopTime-startTime > 500){
        AllTest.updateLogger("[main] WARNING\tHigh response-time detected: " + (stopTime-startTime) + " ms [@element: " + s + "]");
    }
}

如果您需要进一步的代码或信息,请询问。我还有两个更相关的课程。)初始化测试和2.)作为我的测试用例的父类的抽象类。

EN

回答 1

Stack Overflow用户

发布于 2014-08-12 06:38:54

现在我找到解决办法了!

正如您在我的问题代码中所看到的,我重写了FF和IE驱动程序中的findElement方法。这是解决这个真正烦人的问题的关键。因为我发现IE有时很简单,不会单击某些元素(主要是发送ajax请求的元素)。我想这是因为IE没有直接关注这些元素。IE速度慢,需要更多的时间--您需要双击元素(1.设置焦点,2.真正单击)。现在有了解决办法:

您可以从IE中重写findElement方法,并添加一个简单的By-元素来保存请求的元素。如果现在这个方法找不到一个元素,因为没有真正单击父元素,那么您可以在这个方法中再次单击父元素,而vóila就可以工作了!

对于这个解决方案,构建一个自己的类(例如: InternetExplorerDriver2_0)非常重要,它是一个单例类(在自己的类中执行单击),它重写了标准的findElement方法。

(我想很明显,在下面的测试中,您必须使用已更改的IEDriver类而不是原始的'InternetExplorerDriver‘类)

在这里,您可以找到更改后的IEDriver类的代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import static org.junit.Assert.fail;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebElement;

public class InternetExplorerDriver2_0 extends InternetExplorerDriver {

    private static InternetExplorerDriver2_0 instance = null;
    private static DesiredCapabilities caps = null;
    private long startTime;
    private long stopTime;
    private By olderBy;

    private InternetExplorerDriver2_0() {
        super(caps);
    }

    public static synchronized InternetExplorerDriver2_0 getInstance(
            DesiredCapabilities capabilities) {
        if (instance == null) {
            caps = capabilities;
            instance = new InternetExplorerDriver2_0();
        }
        return instance;
    }

    @Override
    public RemoteWebElement findElement(By by) {
        return elementSearch(by, InternetExplorerDriver2_0.getInstance(caps));
    }

    private RemoteWebElement elementSearch(By by, InternetExplorerDriver driver) {
        startTime = System.currentTimeMillis();
        RemoteWebElement helpingElement = null;
        isElementPresent(by);
        try {
            helpingElement = (RemoteWebElement) super.findElement(by);
        } catch (Exception e) {
            System.out.println("[error] method 'elementSearch' incomplete");
            fail("Test not successfull!");
        } finally {
            stopTime = System.currentTimeMillis();
            timeWarning(by.toString());
        }
        olderBy = by;
        return helpingElement;
    }

    private boolean isElementPresent(By by) {
        try {
            super.findElement(by);
            return true;
        } catch (NoSuchElementException e1) {

            //the following lines are the important!!!

            try {
                InternetExplorerDriver2_0.getInstance(caps).findElement(olderBy).click();
                super.findElement(by);
                return true;
            } catch (Exception e2) {
                stopTime = System.currentTimeMillis();
                timeWarning(by.toString());
                AllTest.updateLogger("[main] ERROR\tThe following expression could not be solved: " + by);
                fail("Test not successfull! --> Error: Element not found. Please check the failed XPath's");
                return false;
            }
        }
    }

    private void timeWarning(String s) {
        if (stopTime - startTime > 500) {
            AllTest.updateLogger("[main] WARNING\tHigh response-time detected: " + (stopTime - startTime) + " ms [@element: " + s + "]");
        }
    }
}

我知道这不是一个完美清洁的解决方案,但这是一个很好的解决办法,节省了很多时间!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25261217

复制
相关文章
py+selenium+IE10【IE已停止工作】【已解决】
     手工跑的时候,IE挂,提示“Internet Explorer 已经为了帮助保护您的计算机而关闭此网页”。
逆向小白
2019/03/06
7990
py+selenium+IE10【IE已停止工作】【已解决】
selenium执行click报错的解决方案
操作时可能出现如下提示错误: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <div class="xxx">...</div> is not clickable at point (500, 600). Other element would receive the click: <div class="yyy">...</div>
玖柒的小窝
2021/11/16
1.2K0
python +selenium识别不来click事件,出现报错
elem = browser.find_element_by_id("txtAccount") # Find the query box elem.send_keys("0@cm.com")
北京-宏哥
2019/09/11
9950
Selenium WebDriver使用IE浏览器
IEdriver插件下载地址:http://www.cr173.com/soft/195732.html
周小董
2019/03/25
6.2K0
Selenium WebDriver使用IE浏览器
Selenium底层工作原理
Selenium是ThoughtWorks公司研发的一个强大的基于浏览器的开源自动化测试工具,它通常用来编写web应用的自动化测试。早期也即Selenium1.x时期主要使用Selenium RC(Selenium Remote Control)进来自动化测试。Selenium2.x集成了Selenium和WebDriver的功能。
Meccer
2021/07/20
2.4K0
&nbsp在IE和FireFox中显示不一致
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huyuyang6688/article/details/38704045
DannyHoo
2018/09/13
1.3K0
&nbsp在IE和FireFox中显示不一致
Python+Selenium笔记(二):配置谷歌+IE环境
#有的时候可能要访问外国的网站下载资料或工具,这时可能出现各种问题,例如谷歌人机验证显示不了、网站打不开等,建议使用一个FQ软件 下载免费版的就行了,土豪请随意。下载后直接安装就行了 http://www.softpedia.com/get/Internet/Servers/Proxy-Servers/Lantern.shtml (一)  设置IE (1)   http://docs.seleniumhq.org/download/  下载IEDriverServer。(建议下载32位的,64位的驱动执行
free赖权华
2018/04/27
1.7K0
Python+Selenium笔记(二):配置谷歌+IE环境
SignalR 在IE中无法工作 - Internet Explorer
运行基于SignalR的超线程上载器的代码,发现SignalR 在IE 9上居然没法工作了,提示如下: 提示很明显,需要json2.js的支持。 使用Nuget 搜索json2.js 并安装: 在引用
张善友
2018/01/29
3.3K0
SignalR 在IE中无法工作 - Internet Explorer
Selenium2+python自动化46-js解决click失效问题
前言 有时候元素明明已经找到了,运行也没报错,点击后页面没任何反应。这种问题遇到了,是比较头疼的,因为没任何报错,只是click事件失效了。 本篇用2种方法解决这种诡异的点击事件失效问题 一、遇到的问
上海-悠悠
2018/04/08
2K0
Selenium2+python自动化46-js解决click失效问题
Click fireworks
将下方代码添加到 你当前使用主题的index.ejs 即可加入到首页,我推荐只将其加入到首页,文章页 尽量保持简洁。
BORBER
2019/08/06
1K0
py+selenium遇见IE,元素只有name属性【神奇解决】
IE8的问题:IE8不支持getElementByName,而属性中又没有ID,定位难度较大。
逆向小白
2018/09/12
9450
py+selenium遇见IE,元素只有name属性【神奇解决】
selenium webdriver 启动三大浏览器Firefox,Chrome,IE
     1. 进入Python官方网站:https://www.python.org/downloads/  下载并安装最新版本的Python(建议安装Python3) 。
拓荒者
2019/03/11
1.2K0
selenium webdriver 启动三大浏览器Firefox,Chrome,IE
py+selenium+IE 批量执行脚本10几分钟,IE会卡住【无解,提供绕过方法】
  一个脚本文件里有20几个用例,跑起来10多分钟,每次跑10分钟后(即第22条用例左右时)IE就会卡住,程序就会在那傻等,最后报错超时……不是用例的问题,我注释掉,换其他用例也是一样,所以不是代码的问题。
逆向小白
2019/03/15
8080
py+selenium+IE 批量执行脚本10几分钟,IE会卡住【无解,提供绕过方法】
Python Selenium自动化详解
Selenium,Python的浏览器自动化大佬库,称霸Python浏览器自动化领域。 作为萌新的我,当然要先学习这个既简单又困难的库。
NikoDos
2022/03/29
6550
Python Selenium自动化详解
Python Selenium 自动化详解
注意,阅读本文需要有亿点点前端知识才容易理解。要是大佬看到了不会冒犯到吧,不会吧……
NikoDos
2022/04/20
6510
Python Selenium 自动化详解
谷歌浏览器Chrome与ChromeDriver版本不一致问题全解
在学习使用selenium模块爬取动态渲染信息时,selenium模块需要通过浏览器驱动来控制浏览器的操作。
数据STUDIO
2021/06/24
4.4K0
Selenium-01-测试环境搭建使用
Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好的工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成 .Net、Java、Perl等不同语言的测试脚本。
wencheng
2020/09/28
8241
自动化-Selenium 3-Selenium Grid2(Python版)
Selenium Grid通过同时在多台服务器上运行测试,将Selenium远程控制提升到另一个级别,从而缩短了测试多个浏览器或操作系统所需的时间。
wangmcn
2022/07/22
9600
自动化-Selenium 3-Selenium Grid2(Python版)
python变相调用htmlunit
目前团队使用的自动化测试框架是robotframework+webdriver(IE)+python,据说是从趋势那边搬过来的。webdriver里面,IEdriver是运行最慢的,因此每次跑一遍用例都要花上4个小时
py3study
2020/01/08
1.4K0
点击加载更多

相似问题

selenium的.click函数工作不一致-铬

141

Selenium .NET Click()不工作

12

Python Selenium click()不工作

16

selenium webdriver .click()不工作

330

Selenium click()工作但submit()不工作

10
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文