简体   繁体   English

Selenium / Firefox:命令“.click()”不适用于找到的元素

[英]Selenium / Firefox: Command “.click()” doesn't work with a found element

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.问题:我在 Firefox 中使用 Selenium 和 Java。 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).它每次都在 Google Chrome 中运行良好,有时在 Firefox 中运行良好(使用相同的 Java 代码有时运行,有时不运行)。

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:一些解决方案:

  1. 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.我检查了 xpath 和 cssSelector,并且在这些表达式中找到了独特的元素。

     driver.findElement(By.xpath("//div[@id='size-btn' and not(contains(@class,'opened'))]/span")).click(); // Also checked with By.cssSelector("span.selected-size")
  2. 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();
  3. 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.最后,我有点绝望,我创建了一个新函数来尝试这样做近 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(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:有元素的图像:

元素 1元素 2

Does anybody know how to do that?有人知道怎么做吗?

Finally I found an answer that works with Firefox as well as Google Chrome.最后,我找到了一个适用于 Firefox 和 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.谢谢你阅读我。

I am not sure why are you using this Xpath, if you have freedom to change Xpath then record the element using selenium IDE and use Xpath::position from drop down list of target (it picks unique path relative to html header), it will solve problem of dynamic locator.我不确定您为什么使用此 Xpath,如果您可以自由更改 Xpath,然后使用 selenium IDE 记录元素并使用target下拉列表中的Xpath::position (它选择相对于 html 标头的唯一路径),它将解决动态定位器的问题。 And try below mentioned events.并尝试下面提到的事件。

1- Use clickAt . 1-使用clickAt

2- Use fireevent(focus) and then click. 2- 使用fireevent(focus)然后单击。 Sometime it happens some element in back ground is getting loaded, when it gets loaded, focus move there hence elementNotVisible error.有时会发生背景中的某些元素正在加载,当它加载时,焦点移动到那里,因此 elementNotVisible 错误。

3- Use mouseDownRight . 3-使用mouseDownRight

I have the same problem in Firefox.我在 Firefox 中遇到了同样的问题。 The trick is to click the text inside of not the button itself.诀窍是点击按钮内部的文本而不是按钮本身。

I have some solution, make a class with a robot put there TAB event keys, then call that class.我有一些解决方案,用机器人创建一个类,将 TAB 事件键放在那里,然后调用该类。 What it does its like a back to focus to the page.它的作用就像回到焦点到页面一样。 For some razon the page lost focus and never find that botton.对于某些 razon,页面失去了焦点,永远找不到那个按钮。

   Robot robot;
    try {
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_TAB);
        robot.keyRelease(KeyEvent.VK_TAB);

    } catch (AWTException e) {e.printStackTrace();}

You can try to use the Actions class from org.openqa.selenium.interactions:您可以尝试使用 org.openqa.selenium.interactions 中的 Actions 类:

WebElement element = driver.findElement(By.id("size-btn"));
Actions builder = new Actions(driver);

builder.moveToElement(element).click(element);
builder.perform();
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.click(element);
Action action = actions.build();
action.perform();

This worked for me.这对我有用。

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

相关问题 Selenium 找到元素但随机不点击一个元素,尽管它在 Firefox 上这么说 - Selenium finds element but randomly doesn't click an element although it says so on firefox Selenium Java-如果元素存在,请单击;如果找不到元素,则继续执行下一个命令 - Selenium Java - Click if element exists, continue with the next command if element not found Selenium StaleElementReferenceException错误和页面加载完成代码不是恒定的。 点击元素无效 - Selenium StaleElementReferenceException error & pageload complete code is not constant. Click on element doesn't work Selenium Firefox驱动程序通过测试,但实际上未单击按钮 - Selenium Firefox driver passes test but doesn't actually click button 找到java selenium元素但一键不起作用 - java selenium element found but one click does not work FireFox不是以Selenium开头 - FireFox doesn't start by Selenium ChromeDriver不会单击元素。 硒。 爪哇 - ChromeDriver doesn't click on element. Selenium. Java Selenium找到了Web应用程序的“下一步”按钮,但是click()不起作用 - Selenium finds the “next” button of web application but click() doesn't work 单击事件的排序选项不起作用:Selenium Webdriver - Click event on sort option doesn't work: Selenium Webdriver 当元素已经进入视图时,硒滚动进入视图不起作用 - Selenium Scroll into view doesn't work when element is already into view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM