首页 文章

在javascript中点(chrome)处不能点击Selenium元素

提问于
浏览
0

我是Selenium的新手,我在Browserstack上运行我的selenium脚本 .

一切正常,直到我到达页面底部的10% .

我收到以下错误:

未捕获WebDriverError:Appium错误:未知错误:元素在点(20,324)处无法点击 . 其他元素将收到点击:...(会话信息:chrome = 58.0.3029.83)(驱动程序信息:chromedriver = 2.29.461571(8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5),platform = Linux 3.19.8-100.fc20.x86_64 x86_64)

这是我的代码:

describe.only(testTitle, function () {
    before(function () {
        driver = driverConfiguration.getDriverConfiguration(testTitle, 'chrome')
    })
    after(function (done) {
        driver.quit().then(done)
    })
    it('Sample tests', function (done) {
        driver.get('https://www.test.com').then(function(){
            driver.findElement(webdriver.By.name('cardNumber')).sendKeys('0000000000').then(function(){
                driver.findElement(webdriver.By.id('billingLine1')).sendKeys('test');
                driver.findElement(webdriver.By.id('billingLine2')).sendKeys('test');
                driver.findElement(webdriver.By.id('billingCity')).sendKeys('San Jose');
                driver.findElement(webdriver.By.id('agree')).click(); // ERROR!!!!!
            }).then(function() {
                driver.quit().then(done);
            })
        });
    })
})

When I do the following:

// return driver.wait(function() {
            //     return driver.findElement(webdriver.By.id('agree')).isDisplayed();
            // }, 1000);

它说真的 . 元素是可见的 .

Using Chrome on Samsung Galaxy S8

我不知道如何解决这个问题 .

2 回答

  • 1

    继续上述评论......

    我也遇到了这个问题,当我需要点击一个按钮但它在屏幕上看不到时(但是,它被代码检测到) . 为了解决这个问题,我使用WebDriver的 executeScript() 方法在页面上运行一些JavaScript来滚动,直到我的按钮在视图中 .

    driver.executeScript(`
        var target = document.getElementById('agree');
        var tarTop = target.getBoundingClientRect().top;
        document.body.scrollTop = tarTop;
    `);
    

    如果要为滚动添加超时,可以尝试 driver.executeAsyncScript() ,以确保页面首先到达目的地 . 那时你将使用async / await ......

    await driver.executeAsyncScript(`
        var callback = arguments[arguments.length - 1];
        var target = document.getElementById('agree');
        var tarTop = target.getBoundingClientRect().top;
        document.body.scrollTop = tarTop;
        setTimeout(()=>{ callback( true ); }, 1500);
    `).then((data)=>{
        return data;
    });
    
  • 2

    您已在问题中省略了错误消息中最重要的部分

    其他元素会收到点击:...

    ... 部分中的元素是阻止点击的元素 . 正如您所发现的那样,Selenium报告该元素已显示/可见 . 此错误消息只是声明当Selenium尝试单击该元素时,另一个元素阻止了单击 . 如果您查看阻止元素的HTML并在页面的HTML中搜索,您应该能够识别该元素 . 根据我的经验,它是一个对话框,或者可能是页面底部的 Banner ,等等 . 有时您需要关闭它,有时您需要向下/向上滚动一点以从阻止UI后面获取所需的元素 .

相关问题