【问题标题】:Selenium - The label is clicked (it changes colour for a moment) then the label reverts to the unclicked stateSelenium - 点击标签(它会改变颜色片刻)然后标签恢复到未点击状态
【发布时间】:2018-10-07 03:47:04
【问题描述】:

Screenshot

当我使用 XPath 或 CSS 方法单击一个按钮时,该按钮会突出显示片刻。然后它变回默认颜色,就好像没有选择该选项一样。自动化完成后,我收到一个用户错误,提示未选择按钮选项。

我正在使用最新的 Chrome 驱动程序编写 Java 代码。我也试过 Firefoxdriver。我已经尝试过明确的等待和Thread.sleep,但没有任何效果。 这是代码 - Insurance Cover Type Label

driver.findElement(By.xpath("//*[@id=\'content\']/div[4]/div/div[2]/div[14]/div[2]/ul/li[2]/label")).click();

可以单击和选择屏幕上的类似标签。我搜索了有关此主题的问题和答案,但找不到解决方案。我添加了代码和前端屏幕截图。

【问题讨论】:

  • 似乎由于某种原因,在页面中监听点击的代码崩溃或未注册。虽然很难说为什么没有可重复的例子。这可能是由于之前的操作。在调试模式下逐步尝试并检查浏览器控制台是否有任何错误。
  • 请注意,如果使用文本,更好的定位器将是 by.cssSelector("[for='itemInsured.coverSelected1']")by.xpath(".//*[not(child::*)][normalize-space()='Comprehensive']")

标签: java selenium selenium-webdriver xpath css-selectors


【解决方案1】:

问题是您的定位器不正确(某些索引已关闭),至少当我使用 $x() 在 Chrome 中检查它时。

虽然您可以单击LABEL,但我建议您避免使用长XPath,而是使用包含的INPUT 的ID,然后引用LABEL 兄弟。我试过了,对我来说效果很好。

By.CssSelector("#itemInsured\\.coverSelected1 + label")

【讨论】:

  • 稍微偏离是什么意思?请注意,标签覆盖了输入,这意味着 <input> 不可见/不可交互。
  • @FlorentB。当我导航到该站点并从 Chrome 复制 > 复制 XPath 时(正如我假设 OP 所做的那样),我得到 //*[@id="content"]/div[4]/div/div[1]/div[14]/div[2]/ul/li[1]/label 这与 OP 发布的定位器略有不同......所以,“稍微偏离”。跨度>
  • @FlorentB。你是对的 INPUT 被隐藏了。我正在开发控制台中执行以确认。我已经用与 Selenium 一起工作的代码更新了我的答案。谢谢!
  • @JeffC 你的回答仍然没有解释你所说的定位器稍微偏离是什么意思。尽管您声称 使用与 Selenium 一起使用的代码更新了我的答案,但是您的 CssSelector 中有一个 错误,它不会像您一样工作用(\\) 逃脱了.。您需要将 CssSelector 更改为 By.CssSelector("#itemInsured\.coverSelected1 + label") 以使其工作。
  • @DebanjanB 我在上面的评论中向 Florent 解释了我所说的“稍微偏离”的意思。但这并不重要,不是吗?不,我拥有定位器的方式已正确转义。我运行它并且它有效。自己测试一下。在否决我之前你应该这样做......但你这样做是为了报复,而不是因为它应得的,对吧?
【解决方案2】:

您的代码似乎是在点击标签,而不是在按钮上。即使标签在按钮内,事件也可能在冒泡和取消时被捕获。我会尝试准确选择您要处理事件的对象。

【讨论】:

    【解决方案3】:

    通过查看屏幕截图Insurance Cover Type Label,您想要与之交互的button 元素似乎位于<lable> 标记内。因此,当您单击按钮时,它基本上是单击<label> 而不是<button>。能否请您通过扩展标签提供 dom 的屏幕截图。

    你的定位器应该是这样的

    By.xpath("//*[@id=\'content\']/div[4]/div/div[2]/div[14]/div[2]/ul/li[2]/label/button");
    

    您只需在xpath 中包含引用label 中的按钮元素的元素。

    【讨论】:

      【解决方案4】:

      使用此代码(此 xpath://label[contains(text(), 'Comprehensive')]):

      public class Sof {
      
          /**
           * Specific logger
           */
          private static final Logger logger = LoggerFactory.getLogger(Sof.class);
      
          public static void main(String[] args) throws Exception {
      
              final OperatingSystem currentOperatingSystem = OperatingSystem.getCurrentOperatingSystem();
              String pathWebdriver = String.format("src/test/resources/drivers/%s/googlechrome/%s/chromedriver%s", currentOperatingSystem.getOperatingSystemDir(),
                      SystemArchitecture.getCurrentSystemArchitecture().getSystemArchitectureName(), currentOperatingSystem.getSuffixBinary());
      
              if (!new File(pathWebdriver).setExecutable(true)) {
                  logger.error("ERROR when change setExecutable on " + pathWebdriver);
              }
      
              System.setProperty("webdriver.chrome.driver", pathWebdriver);
              final ChromeOptions chromeOptions = new ChromeOptions();
      
              WebDriver driver = new ChromeDriver(chromeOptions);
              driver.get("https://services.aviva.ie/direct/product/car.htm?_flowId=motor-flow&_flowExecutionKey=e1s1");
              WebElement element = driver.findElement(By.xpath("//label[contains(text(), 'Comprehensive')]"));
              element.click();
              Thread.sleep(5000);
              driver.quit();
          }
      
      }
      

      注意: 如果您使用 Java + Selenium 进行 Web 界面测试,我建议您使用 NoraUi 开源框架

      【讨论】:

        【解决方案5】:

        根据您的代码尝试,您尝试在不接受任何点击的 <label> 标记上调用 click() 方法。

        如果您查看 HTML<label> 节点有 2 个子节点,一个 <input> 和另一个 <label> 节点。您可以在子 <label> 标记上调用 click(),如下所示:

        from selenium import webdriver
        from selenium.webdriver.chrome.options import Options
        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support import expected_conditions as EC
        
        options = Options()
        options.add_argument("start-maximized")
        options.add_argument("disable-infobars")
        options.add_argument("--disable-extensions")
        options.add_argument("--no-sandbox")
        driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\ChromeDriver\chromedriver_win32\chromedriver.exe')
        driver.get('https://services.aviva.ie/direct/product/car.htm?_flowId=motor-flow&_flowExecutionKey=e1s1')
        element =  driver.find_element_by_xpath("//input[@class='radio coverType' and @name='itemInsured.coverSelected' and @value='0101']")
        driver.execute_script("return arguments[0].scrollIntoView(true);", element)
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='itemInsured.coverSelected1']"))).click()
        

        浏览器快照:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-09
          • 2020-02-04
          • 2021-09-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多