从右键单击菜单它不是点击一个特定的选项Selenium
问题描述:
在一个项目中,我想右键点击一个选项,并从那里我想选择“在新窗口中打开链接”。我写了下面的selenium-java代码。在这段代码中,它是在“在新窗口中打开链接”进行爬网,但之后没有单击该选项在新窗口中打开我需要的链接。如果你想要,你可以直接复制和粘贴我的代码来可视化执行流程。请帮我明白的地方是我做了错误的....我的目的是在新窗口中打开链接从右键单击菜单它不是点击一个特定的选项Selenium
package pack_001;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
public class mail {
public static WebDriver driver=new FirefoxDriver();
public static void main(String args[])
{
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.tutorialspoint.com/java/");
driver.manage().window().maximize();
WebElement env=driver.findElement(By.linkText("Java - Environment Setup"));
System.out.println("Env point out");
Actions oAction = new Actions(driver);
oAction.moveToElement(env);
oAction.contextClick(env).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).click().build().perform(); /* this should click on the option*/
}
}
答
HI,而不是点击使用ENTER它将工作
oAction.moveToElement(env);
oAction.contextClick(env).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
也为什么点击给出webElement和新标签选项中是行不通的原因点击点击是不是webElement
在新windwo这必将打开标签
UPDATE
public class DropDownSelection {
public static WebDriver driver=new FirefoxDriver();
public static void main(String[] args) {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// i have used a sample url exactly as similar to your problem
driver.get("http://desirulez.biz/category/wrestlingnetwork/wwe/wwe-raw/");
driver.manage().window().maximize();
// way One
Actions oAction = new Actions(driver);
// now simply hover over the WWE - Menu in your case
WebElement Menu = driver.findElement(By.xpath("//*[@id='menu-item-8']/a"));
oAction.moveToElement(Menu).build().perform();
// hovering will open its sub-menu - Configure in your case
// now identify the sub-menu that you want to click/hover
WebElement Configure = driver.findElement(By.xpath("//*[@id='menu-item-8']/ul/li[7]/a"));
oAction.moveToElement(Configure).build().perform();
// now hovering over it will open its sub menu
// in your case Manage
// now identify the sub-menu that you want to click/hover
WebElement Manage = driver.findElement(By.xpath("//*[@id='menu-item-8']/ul/li[7]/ul/li/a"));
oAction.moveToElement(Manage).click().build().perform();
// way Two
// note if you want to chain above you can even do that like below
oAction.moveToElement(Menu).moveToElement(Configure).moveToElement(Manage).click().build().perform();
}
}
希望这能解决您的查询
要获得同样的效果,你可以创建一个新的'WebDriver'并导航到该链接。例如:'driver2.get(env.getAttribute(“href”));' – Titus
如果您不介意,请您详细说明这一部分。我的意思是,如果我已经确定了一个名为'element'的链接WebElement,我该如何在新窗口中打开它...... – RCode
要创建一个新窗口,您需要创建一个新的'WebDrive',例如:'WebDriver driver2 = new FirefoxDriver();',并在这个新窗口中打开'element'链接,可以这样做'drive2.get(element.getAttribute(“href”));'。如果'element'是一个HTML''元素,这将起作用。另外,Firefox有一个快捷方式可以在新窗口中打开一个链接:SHIFT +左键单击。 – Titus