【问题标题】:getting hidden elements in java using selenium giving error使用selenium在java中获取隐藏元素给出错误
【发布时间】:2016-03-08 00:05:10
【问题描述】:

当我选择隐藏的值时,其显示错误为:

线程“主”org.openqa.selenium.NoSuchElementException 中的异常: 无法定位元素:{“方法”:“部分链接 文本","选择器":"车辆制造"}

这是我的代码:

package section5.advWays.locatingObjects;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;   

public class CusXPathUsingAtt1 {
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        WebDriver wd = new FirefoxDriver();
        wd.manage().window().maximize();
        Thread.sleep(5000);         wd.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
        Select SelectMakedropdown = new Select(wd.findElement(By.id("vehicle-make")));      
        SelectMakedropdown.selectByVisibleText("BMW");
        Select YearSelectDropdown = new Select(wd.findElement(By.id("vehicle-year")));
        YearSelectDropdown.selectByVisibleText("2011");
        Select VehicleSelectDropdown = new Select(wd.findElement(By.id("vehicle-model")));
        VehicleSelectDropdown.selectByVisibleText("228i xDrive Coupe");
    }
    }

如何使用 selenium webdriver 选择那些下拉菜单?

【问题讨论】:

  • 您尝试使用SelectMakedropdown.selectByValue("BMW") 吗?

标签: java selenium selenium-webdriver


【解决方案1】:

有两件事:

  1. 我发现您首先需要单击一个元素,否则选择菜单将无法打开。因此,在我的代码中,我首先单击元素以启用选择菜单。
  2. 还有一些元素不是现成的。例如,除非输入 Make,否则不会启用 Year。

请看下面的代码:

WebDriver driver= new FirefoxDriver();
driver.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
WebDriverWait wait = new WebDriverWait(driver, 30);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Make')]")));
driver.findElement(By.xpath("//div[contains(text(),'Select Make')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-make")));
Select SelectMakedropdown = new Select(driver.findElement(By.id("vehicle-make")));      
SelectMakedropdown.selectByVisibleText("BMW");

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Year')]")));
driver.findElement(By.xpath("//div[contains(text(),'Select Year')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-year")));
Select YearSelectDropdown = new Select(driver.findElement(By.id("vehicle-year")));
YearSelectDropdown.selectByVisibleText("2011");

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Model')]")));
driver.findElement(By.xpath("//div[contains(text(),'Select Model')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-model")));
Select VehicleSelectDropdown = new Select(driver.findElement(By.id("vehicle-model")));
VehicleSelectDropdown.selectByVisibleText("128i Cabriolet Base Model");

driver.quit();

火狐更新:

我尝试了很多,但我仍然无法确定为什么选择在 Firefox 中不起作用。但我仍然设法想出一个解决方法来做必要的事情。在这里,我使用的点击次数更少,而您的应用程序支持的功能更多。

WebDriver driver= new FirefoxDriver();
driver.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
WebDriverWait wait = new WebDriverWait(driver, 30);
JavascriptExecutor executor = (JavascriptExecutor) driver;

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Make')]")));
WebElement we1 = driver.findElement(By.xpath("//div[contains(text(),'Select Make')]"));
executor.executeScript("arguments[0].click();", we1);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-make")));
WebElement SelectMakedropdown = driver.findElement(By.id("vehicle-make"));      
SelectMakedropdown.sendKeys("BMW");
SelectMakedropdown.sendKeys(Keys.ENTER);
Thread.sleep(1000);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Year')]")));
WebElement we2 = driver.findElement(By.xpath("//div[contains(text(),'Select Year')]"));
executor.executeScript("arguments[0].click();", we2);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-year")));
WebElement YearSelectDropdown = driver.findElement(By.id("vehicle-year"));
YearSelectDropdown.sendKeys("2011");
YearSelectDropdown.sendKeys(Keys.ENTER);
Thread.sleep(1000);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Model')]")));
WebElement we3 = driver.findElement(By.xpath("//div[contains(text(),'Select Model')]"));
executor.executeScript("arguments[0].click();", we3);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-model")));
WebElement VehicleSelectDropdown = driver.findElement(By.id("vehicle-model"));
VehicleSelectDropdown.sendKeys("128i Cabriolet Base Model");
VehicleSelectDropdown.sendKeys(Keys.ENTER);

【讨论】:

  • Prateek,它无法正常工作,因为 ElementNotVisibleError 出现错误
  • 哦,这很奇怪 :( 对我来说效果很好。无论如何,我已经改变了一些东西。请现在尝试。
  • 如果可能的话,将确切的代码复制并粘贴到您的 main 中并执行,也可以使用 Chrome 尝试一次。最后,如果您仍然遇到错误。请让我知道您收到错误的元素(以及这次您是否发现任何不同的错误)。
  • Prateek,这些是我的配置:java1.8,eclipse juno 和 FF36.0.4 版本。
  • 谢谢你,我很高兴它有帮助:)
【解决方案2】:

似乎页面需要先点击下拉菜单,试试这个代码:

    wd.findElement(By.xpath("//*[@id='shopByVehicle-search-change']/div[1]/div[1]")).click();

     Select SelectMakedropdown = new Select(wd.findElement(By.id("vehicle-make")));     

     SelectMakedropdown.selectByVisibleText("BMW");

上面肯定会运行。

【讨论】:

  • 再次给出错误:线程“main”org.openqa.selenium.WebDriverException中的异常:ElementNotVisibleError:元素当前不可见,可能无法操作'ElementNotVisibleError:元素当前不可见,可能不调用方法时被操纵':[wdIMouse::click]
  • 请访问该网站,然后您将了解实际问题。
  • 是的,我在 diff 中尝试过。方法并让它工作,请检查更新的答案。
  • 最初它只点击第一个下拉菜单并返回到原始位置(重置)没有选择宝马选项并在控制台中抛出错误。
  • @HelpingHands 您能否粘贴您的整个代码。因为我也遇到了与 khanam 描述的相同的问题。我尝试使用您的代码进行测试,但同样的错误。
猜你喜欢
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 2021-06-15
  • 2012-07-26
  • 1970-01-01
  • 2017-04-16
  • 2017-03-30
相关资源
最近更新 更多