gpt4 book ai didi

java - 从右键单击菜单中,它没有单击特定选项 Selenium

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:24 26 4
gpt4 key购买 nike

在项目中,我想右键单击一个选项,然后从那里选择“在新窗口中打开链接”。我编写了以下 selenium-java 代码。在此代码中,它在“在新窗口中打开链接”上爬行,但之后它不会单击该选项在新窗口中打开我所需的链接。如果您愿意,可以直接复制并粘贴我的代码以可视化执行流程。请帮助我理解我犯的错误在哪里......我的目的是在新窗口中打开链接

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中打开选项卡

更新

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();
}
}

希望这能解决您的疑问

关于java - 从右键单击菜单中,它没有单击特定选项 Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36887869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com