【问题标题】:My code is not working in selenium, help required我的代码在硒中不起作用,需要帮助
【发布时间】:2021-11-30 06:30:47
【问题描述】:

地址和帐单地址有 2 个字段,这两个字段都会抛出建议。

如果我对帐单地址代码有效的地址代码发表评论,我的代码就可以了。 如果我对帐单地址代码发表评论,则地址代码有效。 但是当我注释掉你所知道的一切时无法运行它?代码如下。

 //adding address and select from the suggestion bar here I'm selecting with ID
          driver.findElement(By.id("entityAddress_0")).sendKeys("Khalifa city sector 13");
          driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
         List <WebElement> suggestion = driver.findElements(By.className("pac-item")); //here I print all the value which are coming 
         
       //here I'm using for each loop and print the value so we can see all the value which appeared in suggestions
         for (WebElement suggest : suggestion) {
              if(suggest.getText().equalsIgnoreCase("Sector 13Khalifa City - Abu Dhabi - United Arab Emirates")) {
                  suggest.click();
              break;
          }
              
              Thread.sleep(1000);
              
              //now adding the billing address
             driver.findElement(By.id("entityAddress_01")).sendKeys("Khalifa city sector 13");
             driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
            List <WebElement> suggestions =driver.findElements(By.className("pac-item-query"));
            for (WebElement suggests : suggestions) {
                //System.out.println(suggests.getText());
                if(suggests.getText().equalsIgnoreCase("Khalifa city"));
                suggests.click();
                
            }
    }}}

【问题讨论】:

  • 请始终描述当前和期望的行为,并包括堆栈跟踪和页面源代码或 url。
  • 在端口 45380 上启动 ChromeDriver 94.0.4606.61 (418b78f5838ed0b1c69bb4e51ea0252171854915-refs/branch-heads/4606@{#1204}) 只允许本地连接。请参阅chromedriver.chromium.org/security-considerations 获取有关确保 ChromeDriver 安全的建议。 ChromeDriver 已成功启动。 2021 年 10 月 13 日上午 11:50:46 org.openqa.selenium.remote.ProtocolHandshake createSession INFO:检测到的方言:W3C 当我注释掉上面的代码时代码不起作用,然后它就起作用了。当我想完成流程并注释掉代码时,它就会停止。
  • 如果某个答案对您有帮助,请选择它。

标签: java selenium automation


【解决方案1】:

当您声明suggestions 两次时,您遇到了 java 错误。

您不能两次声明List &lt;WebElement&gt; suggestions = driver.findElement();。您可以进行第一次声明,但对于第二次声明,即计费声明,您需要删除 List &lt;WebElement&gt; 并为第二次声明进行 suggestions = driver.findElement(//your xpath)

两个不同的suggestions 声明应该看起来完全一样:

List <WebElement> suggestions = driver.findElements(By.className("pac-item"));

suggestions = driver.findElements(By.className("pac-item-query"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多