简体   繁体   English

当您的页面javascript重新加载元素时,如何应用等待时间

[英]How to apply wait time when Your page javascript reloads the element

Hi i am using selenium webdriver to automate my script and i have used wait.until condition in my script to click on the delivery bttn in below html page. 嗨,我正在使用Selenium Webdriver自动执行我的脚本,并且我在脚本中使用了wait.until条件来单击html页面下方的传递bttn。 The problem is selenium is finding my element but since the java script reloads the certain element. 问题是硒正在找到我的元素,但是由于Java脚本重新加载了某些元素。 And the delivery bttn only becomes clickable after reloading. 并且交付bttn仅在重新加载后才可单击。 And my selenium script throws "stale element reference: element is not attached to the page document". 我的硒脚本抛出“陈旧元素引用:元素未附加到页面文档中”。 What should i do to overcome this error. 我应该怎么做才能克服这个错误。

WebElement delibttn=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("(//button[@class='btn-to-cart nopricetohide btn btn-primary your-catalog-deliver btn-block btn-unpadded tocart-rounded'])[1]")));
delibttn.click();
WebElement contshopping=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='btn btn-link full-width mb-10']")));

Screenshot: 截图:

htm代码的屏幕截图

there are two ways to solve your problem. 有两种方法可以解决您的问题。

1) Run the code with Async, that way you can 'await' a line of code, for example.. 1)使用Async运行代码,例如,您可以“等待”一行代码。

 function async(test1){
    await driver.findElement(By.id("");
    driver.click();
    });

or you can also do the following 或者您也可以执行以下操作

2) 2)

 function (test1) {
        let element = driver.findElement(By.id(elementId));
        driver.wait(until.elementIsVisible(element), 10000).then(async () =>{
            element.click();
    });

This wait in number 2, is the one that i use in my code and it always works. 第2次等待是我在代码中使用的等待,它始终有效。

A very barbaric way of doing it would be to add a ridiculous wait time to check that it isn't something else showing an error similar to a wait problem 一种非常野蛮的方法是添加一个荒谬的等待时间,以检查它是否不是显示类似于等待问题的错误的其他东西

driver.sleep(10000);
or 
thread.sleep(10000);

(the measurement is in milliseconds unless defined otherwise) (除非另有定义,否则度量单位为毫秒)

Please let me know if these solutions do not solve the problem. 如果这些解决方案不能解决问题,请告诉我。

as Jack suggested you could use async, but I always used an infinte while loop 正如杰克建议的那样,您可以使用异步,但是我总是使用无限循环

Code i have given below is in python, but you can use the logic in java too 我下面给出的代码在python中,但是您也可以在java中使用逻辑

def wait_for_element():
    val = True
    while val:
        web_elem = driver.find_element_by_id('id')
         try:
            web_elem.is_displayed()
         except Exception as ex:
            val = True
         else:
            val = False

i know infinite loop is not a better way than async, but if there are cases where you can't use async you can use this. 我知道无限循环不是比异步更好的方法,但是如果在某些情况下不能使用异步,则可以使用异步。 Also keep in mind to put timeout for loop, otherwise you would looping infinitely when the page was unresponsive or has not loaded. 还要记住将超时用于循环,否则当页面无响应或尚未加载时,您将无限循环。

the reason it is still throwing this issue is because you are not handling your exceptions properly, this is a response to it still throwing stale element errors. 仍然引发此问题的原因是因为您没有正确处理异常,这是对它仍然引发陈旧元素错误的一种响应。

Add something like this to your project, if you look at the bottom of my code you will see that i have added exceptions to catch errors so it does not affect the code the way it is doing. 在您的项目中添加类似的内容,如果您查看代码的底部,您会发现我已经添加了异常来捕获错误,因此它不会像以前那样影响代码。

  driver.findElement(By.id(buttonID)).then(pageElement => {                           
        driver.wait(until.elementIsVisible(pageElement), 10000).then( () => {                                                          
            pageElement.click();
                next();
            })
            .catch(ex => {
                console.log(ex.message, ex.stack)
            });
    }).catch(ex => {console.log(ex.message, ex.stack)});

This is the example of how i am using catches, however many promises you have in your function the more catches you will need, if you hover over an element in Visual Code / Studio you will be able to see if it throws a promise or not. 这是我如何使用catch的示例,但是功能中具有的promise越多,所需的catch越多,如果将鼠标悬停在Visual Code / Studio中的某个元素上,您将能够查看它是否抛出了promise 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM