Selenium with webdriver Internet Explorer 11



我在Python for Internet explorer 11中使用Selenium来自动执行某些任务。我最近发现点击操作不稳定。因为所有工作都可以在我的电脑中找到,所以可以单击该按钮。例如:

driver.find_element_by_id(...).click()

但是当我将程序移动到另一台计算机时,无法单击按钮并发生错误,我必须将 click(( 更改为类似

driver.find_element_by_id(...).send_keys(Keys.SPACE) or
element=driver.find_element_by_id(...).send_keys(Keys.SPACE)
driver.execute_script("arguments[0].click();", element)

或者我必须尝试其他方法有没有人有同样的问题,你知道是什么原因吗?因为代码在您的计算机上很好很烦人,但我觉得与他人共享并不安全。谢谢

在不同的机器上执行时,我在使用 Internet Explorer 时遇到了这个问题,我使用的解决方法(它应该跨浏览器工作非常通用(是显式等待 WebElement(在您的情况下为按钮(可见并可单击,然后再尝试在其上执行单击事件。

因此,代码如下所示:

WebDriverWait = new WebDriverWait(driver, 10(;

    try {
        wait.until(ExpectedConditions.visibilityOfElementLocated(
                By.xpath("Your path")));
        wait.until(ExpectedConditions
                .elementToBeClickable(By.xpath("The same path")));
    } catch (Exception e) {
        System.err.println("The error message to be displayed in console: "
                + e.getStackTrace());
    } finally {
        driver.findElement(By.xpath("The same path")).click();
    }

上面的代码是用Java编写的,但前提在Python中也是如此。

最新更新