Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何找到合适的复选框定位器_Java_Selenium_Checkbox - Fatal编程技术网

Java 如何找到合适的复选框定位器

Java 如何找到合适的复选框定位器,java,selenium,checkbox,Java,Selenium,Checkbox,我正在用Selenium编写一个自动测试,我的定位器有问题。网址:。我找不到最后三个复选框的正确定位器。根据我使用的定位器,我会得到一个“错误”元素“不可交互”,或者勾选复选框,但在新选项卡中打开链接“阅读更多”下的页面。我没有这个问题与复选框没有“阅读更多”的链接,请帮助 public void clickDiscountsCheckBox(){ WebElement discountsCheckBox = driver

我正在用Selenium编写一个自动测试,我的定位器有问题。网址:。我找不到最后三个复选框的正确定位器。根据我使用的定位器,我会得到一个“错误”元素“不可交互”,或者勾选复选框,但在新选项卡中打开链接“阅读更多”下的页面。我没有这个问题与复选框没有“阅读更多”的链接,请帮助

public void clickDiscountsCheckBox(){
        WebElement discountsCheckBox = driver
                                       .findElement(By.xpath("//*[@id=\"id_terms_0\"]"));
        discountsCheckBox.click();
以下是我得到的错误:

org.openqa.selenium.ElementNotVisibleException:元素不可交互 (会话信息:chrome=71.0.3578.98) (驱动程序信息:chromedriver=2.45.615291(ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),平台=Windows NT 10.0.17134 x8664)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:0毫秒


我查看了提供的URL,发现最后三个要单击的复选框是“::before”,这是一个伪元素。你试过JavascriptExecutor吗?您可以尝试使用CSSSelector来标识元素。

试试这个。这样应该可以

driver.get("https://talixo.pl/register/?next=/");
            Thread.sleep(4000);
            List<WebElement> listOfElements = 
driver.findElements(By.xpath("//span[@class='label-body']"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
for (int i=0; i<listOfElements.size();i++){
               executor.executeScript("arguments[0].click();",listOfElements.get(i));               
              }
driver.get(“https://talixo.pl/register/?next=/");
睡眠(4000);
元素列表=
findElements(By.xpath(//span[@class='label-body']);
JavascriptExecutor executor=(JavascriptExecutor)驱动程序;

for(int i=0;我希望得到建议,但不幸的是,它也不起作用:(这应该单击3个“阅读更多”复选框。太好了!如果您需要任何进一步的帮助,请告诉我。我知道您现在不能投票给我。如果您有机会,请投票给我,以便我在这次论坛上更感兴趣地解决查询。我不知道。将来我会。
Thread.sleep()
是一种不好的做法,不应使用。当可以通过正常方式单击复选框时,您还应避免使用JSE来单击复选框。当您使用JSE与页面交互时,您需要意识到您不再是一个用户。JSE可以以任何用户都无法单击或不可见的方式单击或以其他方式与页面交互隐藏的元素等。为什么要使用XPath仅通过ID来定位元素?为什么不使用
by.ID(“ID\u terms\u 0”)
?另外,您是否尝试等待clickable?