【问题标题】:How to click on a check box in a new window through selenium webdriver如何通过 selenium webdriver 在新窗口中单击复选框
【发布时间】:2018-10-26 03:07:31
【问题描述】:

我正在尝试点击此复选框,但我无法点击该复选框。

请把 xpath 或 css 发给我

我试过了:

xpath
//div//*[@id='thCheckBox']

.//*[@id='searchOrderModel']/div/div/div[3]/div/button[2]

【问题讨论】:

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


    【解决方案1】:

    正如您所提到的,它是新窗口,那么您必须切换 到新的windows 才能与网络元素进行交互。到达新窗口后,您可以使用此代码:

    ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());  
    driver.switchTo().window(tabs.get(1));
    

    //现在您已经专注于新的Windows,您现在可以与复选框进行交互:

    WebElement checkBox = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("thCheckBox")));  
    checkBox.click();  
    

    然后,如果您愿意,您必须再次切换回您访问此页面的页面。

    driver.close();
    driver.switchTo.windows(tabs.get(0));
    

    【讨论】:

      【解决方案2】:

      根据您提供的 HTML 和您作为 Check Box 的问题位于 Check Boxclick() 的新窗口中,您必须诱导 WebDriverWait 使 Check Box 可点击,您可以使用以下 Locator Strategy

      • cssSelector

        "table.table.table-hover.dataTable#orderNoDropdown label[for='thCheckBox']"
        
      • xpath

        "//table[@class='table table-hover dataTable' and @id='orderNoDropdown']//label[@for='thCheckBox']"
        

      【讨论】:

        【解决方案3】:

        复选框旁边有一个标签。您可以尝试单击标签,然后它会使用下面的 x-path 来单击复选框。

        //label[@for='thCheckbox']
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-04-24
          • 2018-11-14
          • 1970-01-01
          • 1970-01-01
          • 2015-12-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多