[英]How to wait for an item to finish loading?
Well, I'm testing on a page designed in Angular and Java with Selenium.好吧,我正在用 Selenium 用 Angular 和 Java 设计的页面上进行测试。 When a query is made to the database or the page loads in its code this appears:
当对数据库进行查询或页面在其代码中加载时,会出现:
<span _ngcontent-c0 class = "loading"> </span>
When it finishes loading, it changes like this:当它完成加载时,它会变成这样:
<span _ngcontent-c0 class = "loading" hidden> </span>
My problem is that this "loading" is intercepting the clicks that I sent in the test:我的问题是这个“加载”拦截了我在测试中发送的点击:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <a class="white-text" id="aaidEntidadBancaria" title="Apply"> ... </a> is not clickable at point (460, 502) . Other element would receive the click: <span _ngcontent-c0 = "" class = "loading"> </span>
what kind of wait could i use?我可以使用什么样的等待? I already tried invisibilityOfElementLocated (locator) but it didn't work ...
我已经尝试过 invisibilityOfElementLocated (locator) 但它没有用......
You need to wait for the loader to be invisible or hidden inducing WebDriverWait for the invisibilityOfElementLocated()
and you can use either of the following Locator Strategies :您需要等待加载器不可见或隐藏,从而为
invisibilityOfElementLocated()
引入WebDriverWait ,您可以使用以下任一定位器策略:
cssSelector
: cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("span.loading)));
xpath
: xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//span[@class='loading'])));
You can find a relevant discussion in Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working
您可以在Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working 中找到相关讨论
Once the loader is invisible or hidden you can induce WebDriverWait for the elementToBeClickable()
and invoke click()
as follows:一旦加载器不可见或隐藏,您可以为
elementToBeClickable()
引入WebDriverWait并调用click()
如下:
cssSelector
: cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("span.loading))); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.white-text#aaidEntidadBancaria[title='Apply']"))).click();
xpath
: xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//span[@class='loading']))); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='white-text' and @id='aaidEntidadBancaria'][@title='Apply']"))).click();
You can find a relevant discussion in Element MyElement is not clickable at point (x, y)… Other element would receive the click
您可以在元素中找到相关讨论MyElement is not clickable at point (x, y)... 其他元素会收到点击
If invisibility_of_element_located
is not working for you, you can try to work around the ClickIntercepted
issue by using Javascript click:如果
invisibility_of_element_located
不适合您,您可以尝试使用 Javascript 单击来解决ClickIntercepted
问题:
# locate clickable element
clickable_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//a[@title='Apply']")))
# click with Javascript
driver.execute_script("arguments[0].click();", clickable_element)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.