Selenium / Firefox:Command“.click()”不适用于找到的元素 [英] Selenium / Firefox: Command ".click()" doesn't work with a found element
问题描述
问题:我我在Firefox中使用Selenium和Java。我需要找到一个元素(一个列表框)并点击它。所以,代码找到元素,但点击操作不起作用。它每次都能在Google Chrome中正常工作,有时甚至可以在Firefox中使用(有时使用相同的Java代码,有时不起作用)。
当程序进入页面时,代码的一部分与元素:
< div id =size- btnclass =size-btn>
< span class =selected-size> SELECCIONA TALLA< / span>
< div class =size-selectstyle =display:none;>
< table>
< tbody>
< tr id =selecsize_2class =product-sizedata-ga-props ={action:'Seleccionar_Producto',opt_label:'Escoger_Talla'}data-catentryid =1051607>
< tr id =selecsize_3class =product-sizedata-ga-props ={action:'Seleccionar_Producto',opt_label:'Escoger_Talla'}data-catentryid =1051608>
< / tbody>
< / table>
< button class =size-guide gaViewEvent gaTrackdata-ga-props ={action:'Seleccionar_Talla',opt_label:'Guia_de_tallas'}data-href =http://www.anyweb。 com / webapp / wcs / stores / servlet / ProductGuideSizeAjaxView?catalogId = 24052& categoryId = 358056& langId = -5& productId = 1047599& storeId = 10701>Guíade tallas< / button>
< / div>
< / div>
单击元素时,代码的一部分发生变化:
< div id =size-btnclass =size-btn opened>
我尝试了很多解决方案,有时可以运行,但是下次运行程序时,再次工作。
一些解决方案:
-
,但不会运行点击操作。我使用xpath和cssSelector进行了检查,并且在这些表达式中找到了独特的元素。
driver.findElement(By.xpath( / / div [@ id ='size-btn'and not(contains(@ class,'opened'))] / span))。click(); //也检查By.cssSelector(span.selected-size)
-
虽然是因为时间,所以我试图解决这个问题。
WebElement we = driver.findElement(By。 xpath(// div [@ id ='size-btn'and not(contains(@ class,'opened'))] / span)); // By.cssSelector(span.selected-size)
Thread.sleep(3000);
we.click();
-
最后,我有点绝望了,我创建了一个新函数来尝试做这个近60次,寻找元素代码的变化,如果有任何改变,只是试图再次点击行动。
clickAndWaitWhileElementIsNotPresent(By.xpath(// div [@ id ='size-btn'and not(contains(@ class,'opened'))] / span),By.xpath(// div [ @ class ='size-btn opened'] / span)); // By.cssSelector(span.selected-size)
private void clickAndWaitWhileElementIsNotPresent(By1,By2)throws Exception {
for(int second = 0 ;; second ++){
if(second> = 60)
fail(timeout);
尝试{
if(isElementPresent(by2))
{
break;
}
else
{
driver.findElement(by1).click();
} catch(Exception e){
}
Thread.sleep(1000);
code
元素的图像:
任何人都知道如何做到这一点?
解决方案最后,我找到了一个适用于Firefox和Google Chrome的答案。 >
WebElement we = this.driver.findElement(By.id(size-btn));
JavascriptExecutor executor =(JavascriptExecutor)驱动程序;
executor.executeScript(arguments [0] .click();,we);
waitForElementPresent(By.xpath(// div [@ id ='size-btn'and contains(@ class,'opened')] / span));
感谢您的阅读。
I tried to find a solution to this thing and I spent a lot of time, but it is almost imposible to me to do that.
The matter: I am using Selenium with Java in Firefox. I need to find an element (a listbox) and click on it. So, the code finds the element, but click action does not work. It works fine in Google Chrome every time, and just sometimes in Firefox (with the same Java code sometimes works, and sometimes does not).
There is the part of code with the element when the program enters on the page:
<div id="size-btn" class="size-btn"> <span class="selected-size">SELECCIONA TALLA </span> <div class="size-select" style="display: none;"> <table> <tbody> <tr id="selecsize_2" class="product-size" data-ga-props="{action:'Seleccionar_Producto', opt_label:'Escoger_Talla'}" data-catentryid="1051607"> <tr id="selecsize_3" class="product-size" data-ga-props="{action:'Seleccionar_Producto', opt_label:'Escoger_Talla'}" data-catentryid="1051608"> <tr id="selecsize_4" class="product-size" data-ga-props="{action:'Seleccionar_Producto', opt_label:'Escoger_Talla'}" data-catentryid="1051609"> <tr id="selecsize_5" class="product-size" data-ga-props="{action:'Seleccionar_Producto', opt_label:'Escoger_Talla'}" data-catentryid="1051610"> </tbody> </table> <button class="size-guide gaViewEvent gaTrack" data-ga-props="{action:'Seleccionar_Talla', opt_label:'Guia_de_tallas'}" data-href="http://www.anyweb.com/webapp/wcs/stores/servlet/ProductGuideSizeAjaxView?catalogId=24052&categoryId=358056&langId=-5&productId=1047599&storeId=10701">Guía de tallas</button> </div> </div>
And there is the part of code that changes when the element is clicked:
<div id="size-btn" class="size-btn opened">
I tried many solutions and sometimes it works, but the next time I run the program, it does not work again.
Some solutions:
It finds the element, but does not run click action. I checked with xpath and cssSelector, and there are unique elements found with those expressions.
driver.findElement(By.xpath("//div[@id='size-btn' and not(contains(@class,'opened'))]/span")).click(); // Also checked with By.cssSelector("span.selected-size")
I though it was because of the time, so I tried to solve it that way.
WebElement we = driver.findElement(By.xpath("//div[@id='size-btn' and not(contains(@class,'opened'))]/span")); // By.cssSelector("span.selected-size") Thread.sleep(3000); we.click();
Finally, I was a little bit desperate, and I created a new function to try to do this almost 60 times, looking for the change on the element code and if there was any change, just tried to do click action again.
clickAndWaitWhileElementIsNotPresent(By.xpath("//div[@id='size-btn' and not(contains(@class,'opened'))]/span"),By.xpath("//div[@class='size-btn opened']/span")); // By.cssSelector("span.selected-size") private void clickAndWaitWhileElementIsNotPresent(By by1, By by2) throws Exception { for (int second = 0;; second++) { if (second >= 60) fail("timeout"); try { if (isElementPresent(by2)) { break; } else { driver.findElement(by1).click(); } } catch (Exception e) { } Thread.sleep(1000); } }
There are the images of the element:
Does anybody know how to do that?
解决方案Finally I found an answer that works with Firefox as well as Google Chrome.
WebElement we = this.driver.findElement(By.id("size-btn")); JavascriptExecutor executor = (JavascriptExecutor) driver; executor.executeScript("arguments[0].click();", we); waitForElementPresent(By.xpath("//div[@id='size-btn' and contains(@class,'opened')]/span"));
Thanks for reading me.
这篇关于Selenium / Firefox:Command“.click()”不适用于找到的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!