Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.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/3/html/86.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 如何使用webdriver从下拉菜单中选择webelement_Java_Html_Selenium_Drop Down Menu_Selenium Webdriver - Fatal编程技术网

Java 如何使用webdriver从下拉菜单中选择webelement

Java 如何使用webdriver从下拉菜单中选择webelement,java,html,selenium,drop-down-menu,selenium-webdriver,Java,Html,Selenium,Drop Down Menu,Selenium Webdriver,我试图从下拉列表中选择一个webelement,但无法这样做。 我试图从下拉列表中选择的webelement是 编辑/查看简历 第一次尝试 在我的第一次尝试中,我尝试使用Action和select类方法来选择webelement。在这里,为了从下拉列表中选择元素,我尝试了selectByValue(值)、selectByIndex(1)和selectByVisibleText(文本),但它们都不起作用,我得到了org.openqa.selenium.support.ui.UnexpectedT

我试图从下拉列表中选择一个webelement,但无法这样做。 我试图从下拉列表中选择的webelement是 编辑/查看简历


第一次尝试 在我的第一次尝试中,我尝试使用Action和select类方法来选择webelement。在这里,为了从下拉列表中选择元素,我尝试了selectByValue(值)、selectByIndex(1)和selectByVisibleText(文本),但它们都不起作用,我得到了org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应该是“选择”的,但它是“a”此异常

  Webelement wb = driver.findElement(By.xpath("//div[@class='ns_menu_item_wrap ns_lt active']/a"));
  Actions mouse = new Actions(driver);
  mouse.moveToElement(wb).perform();
  Select sel = new Select(wb);
  sel.selectByIndex(1);
  Webelement wb = driver.findElement(By.xpath("//div[@class='ns_menu_item_wrap ns_lt active']/a"));
  Actions mouse = new Actions(driver);
  mouse.moveToElement(wb).click();
  driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS);
  wb=driver.findElement(By.xpath("//a[contains(@href,'http://my.monsterindia.com/view_resume.html?mode=edit')]"));
  mouse.moveToElement(wb).click();
  driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS);
  mouse.build();
  Thread.sleep(4000);
  mouse.perform(); 

第二次尝试 在这里,我试图通过使用Action类build()、click()和perform()方法来选择元素,但什么也没有发生,也就是说,我仍然无法从下拉列表中选择webelement。对于这段代码,我没有得到任何例外

  Webelement wb = driver.findElement(By.xpath("//div[@class='ns_menu_item_wrap ns_lt active']/a"));
  Actions mouse = new Actions(driver);
  mouse.moveToElement(wb).perform();
  Select sel = new Select(wb);
  sel.selectByIndex(1);
  Webelement wb = driver.findElement(By.xpath("//div[@class='ns_menu_item_wrap ns_lt active']/a"));
  Actions mouse = new Actions(driver);
  mouse.moveToElement(wb).click();
  driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS);
  wb=driver.findElement(By.xpath("//a[contains(@href,'http://my.monsterindia.com/view_resume.html?mode=edit')]"));
  mouse.moveToElement(wb).click();
  driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS);
  mouse.build();
  Thread.sleep(4000);
  mouse.perform(); 

下拉选项的HTML片段
您提供的标记中没有HTML
选择项。查看详细信息。你那里只有链接。那你为什么不点击那个链接呢。这大概是:

driver.findElement(By.linkText("Job Agent")).click();

或者您需要单击的任何一个。

您的第二次尝试看起来有点奇怪,因为在您的操作中,您调用了
鼠标.moveToElement(wb)
,然后在执行该操作之前将另一个元素分配给
wb
。我想尝试以下方法:

driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS); // It is enough to call this once
Webelement wb = driver.findElement(By.xpath("//div[@class='ns_menu_item_wrap ns_lt active']/a"));
Actions mouse = new Actions(driver);
mouse.moveToElement(wb).click();
Webelement wb2 = driver.findElement(By.xpath("//a[contains(@href,'http://my.monsterindia.com/view_resume.html?mode=edit')]"));
mouse.moveToElement(wb2).click();
mouse.build(); // You don't need the sleep after this before calling perform()
mouse.perform(); 
但是像埃尔金一样。建议,我不明白为什么一个简单的

driver.findElement(By.xpath("//a[contains(@href,'http://my.monsterindia.com/view_resume.html?mode=edit')]"));

无法工作。

因为我无法将代码粘贴到注释中,所以将其作为答案- 在这段代码中,我得到System.out.println(“2”)的输出;as 2,但未获得System.out.println(“3”)的任何输出; 我在逻辑上犯了错误吗

  driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS);
  wb = driver.findElement(By.linkText("My Monster"));
  Actions mouse = new Actions(driver);
  mouse.moveToElement(wb).click();
  System.out.println("2");
  WebElement wb1 = driver.findElement(By.linkText("Edit/View Resume"));
  mouse.moveToElement(wb1).click();
  mouse.build();
  mouse.perform();
  System.out.println("3");

当我按照您的建议在try1中更改代码时,我收到org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应该是“select”,但却是“a”异常。当我在try2中更改代码时,也就是说,没有得到结果,也没有任何例外。我意识到我的错误,并按照你的建议做了完全相同的事情,但它仍然没有点击网页元素,也没有给出任何例外。我无法将我的代码作为注释,这就是为什么我将其作为答案。请检查一遍当您对
moveToElement
mouse
时,当涉及多个操作时,您需要使其
build
perform
。在这里,您第一次错过了这个
moveToElement
@nitin chawda我添加了mouse.build();和鼠标;对于第一个moveToElement,但仍然是System.out.println(“3”);没有被打印出来