Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.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使用Selenium右键单击链接并在新选项卡中打开链接_Java_Selenium_Selenium Webdriver_Tabs_Webdriverwait - Fatal编程技术网

如何通过Java使用Selenium右键单击链接并在新选项卡中打开链接

如何通过Java使用Selenium右键单击链接并在新选项卡中打开链接,java,selenium,selenium-webdriver,tabs,webdriverwait,Java,Selenium,Selenium Webdriver,Tabs,Webdriverwait,我正在尝试右键单击忘记的帐户?使用Selenium在Facebook登录页面上链接,但它不起作用 我试图在contextClick()之后send.Keys(),但按键发生在页面上,而不是上下文菜单上 package keyboardandmouseaction; import java.awt.AWTException; import java.util.Iterator; import java.util.Set; import org.openqa.selenium.By; impor

我正在尝试右键单击忘记的帐户?使用Selenium在Facebook登录页面上链接,但它不起作用

我试图在
contextClick()
之后
send.Keys()
,但按键发生在页面上,而不是上下文菜单上

package keyboardandmouseaction;

import java.awt.AWTException;
import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class testcase8 {
    public static void main(String[] args) throws AWTException, InterruptedException {

        System.out.println("Running keyboardandmouseactions > testcase8");

        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();

        driver.manage().window().maximize();
        driver.get("https://www.facebook.com/");

        WebElement link=driver.findElement(By.xpath("//a[contains(text(),\"Forgotten account?\")]"));
        Actions a=new Actions(driver);

        // defective code start
        Action builder=a.moveToElement(link).contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build();
        // defective code end
        builder.perform();



        Set<String> windowid =driver.getWindowHandles();
        Iterator<String> itr =windowid.iterator();

        String mainwindow=itr.next();
        String childwindow=itr.next();
        System.out.println("The mainwindow id is "+mainwindow);
        System.out.println("The childwindow id is "+childwindow);
        driver.switchTo().window(childwindow);
        driver.get("http://demo.automationtesting.in/Alerts.html");
        driver.close();

}
}
封装键盘和鼠标动作;
导入java.awt.AWTException;
导入java.util.Iterator;
导入java.util.Set;
导入org.openqa.selenium.By;
导入org.openqa.selenium.Keys;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.interactions.Action;
导入org.openqa.selenium.interactions.Actions;
公共类测试用例8{
publicstaticvoidmain(字符串[]args)抛出AWTException、interruptedeexception{
System.out.println(“运行键盘和鼠标操作>测试用例8”);
System.setProperty(“webdriver.chrome.driver”,“D:\\chromedriver\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
driver.manage().window().maximize();
驱动程序。获取(“https://www.facebook.com/");
WebElement link=driver.findElement(By.xpath(“//a[contains(text(),\“忘记的帐户?\”)”));
动作a=新动作(驾驶员);
//有缺陷的代码启动
Action builder=a.moveToElement(link).contextClick(link).sendKeys(键.箭头向下)。sendKeys(键.回车).build();
//缺陷代码端
builder.perform();
设置windowid=driver.getWindowHandles();
迭代器itr=windowid.Iterator();
字符串mainwindow=itr.next();
字符串childwindow=itr.next();
System.out.println(“主窗口id为”+mainwindow);
System.out.println(“子窗口id为”+childwindow);
driver.switchTo().window(childwindow);
驱动程序。获取(“http://demo.automationtesting.in/Alerts.html");
driver.close();
}
}
您可以按住ctrl键并单击()在新选项卡中打开链接,而不是右键单击链接并在新选项卡中打开链接,最后使用以下命令切换到新选项卡:

  • 代码块:

    import java.util.Collections;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Control_Click {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
            options.setExperimentalOption("useAutomationExtension", false);
            WebDriver driver =  new ChromeDriver(options);
            driver.get("https://www.facebook.com/");
            WebElement forgotPassword = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Forgotten account?")));
            String parentWindow = driver.getWindowHandle();
            System.out.println("The mainwindow handle is "+driver.getWindowHandle());
            new Actions(driver).keyDown(Keys.CONTROL).click(forgotPassword).keyUp(Keys.CONTROL).build().perform();
            new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
            for(String window:driver.getWindowHandles()) {
                if(!parentWindow.equalsIgnoreCase(window)) {
                    driver.switchTo().window(window);
                    System.out.println("The childwindow id is "+driver.getWindowHandle());
                    driver.get("http://demo.automationtesting.in/Alerts.html");
                }
            }
        }
    }
    
  • 控制台输出:

    The mainwindow handle is CDwindow-0753C465F9132427837081CE5AB8C67D
    The childwindow id is CDwindow-79C688CE476CA8EC4729EFFDE93C84EA
    
  • 浏览器快照:


参考文献 您可以在以下内容中找到一些相关的详细讨论:

不必右键单击链接并在新选项卡中打开链接,您可以按ctrl键并
单击()
在新选项卡中打开链接,最后使用以下命令切换到新选项卡:

  • 代码块:

    import java.util.Collections;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Control_Click {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
            options.setExperimentalOption("useAutomationExtension", false);
            WebDriver driver =  new ChromeDriver(options);
            driver.get("https://www.facebook.com/");
            WebElement forgotPassword = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Forgotten account?")));
            String parentWindow = driver.getWindowHandle();
            System.out.println("The mainwindow handle is "+driver.getWindowHandle());
            new Actions(driver).keyDown(Keys.CONTROL).click(forgotPassword).keyUp(Keys.CONTROL).build().perform();
            new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
            for(String window:driver.getWindowHandles()) {
                if(!parentWindow.equalsIgnoreCase(window)) {
                    driver.switchTo().window(window);
                    System.out.println("The childwindow id is "+driver.getWindowHandle());
                    driver.get("http://demo.automationtesting.in/Alerts.html");
                }
            }
        }
    }
    
  • 控制台输出:

    The mainwindow handle is CDwindow-0753C465F9132427837081CE5AB8C67D
    The childwindow id is CDwindow-79C688CE476CA8EC4729EFFDE93C84EA
    
  • 浏览器快照:


参考文献 您可以在以下内容中找到一些相关的详细讨论:

WebDriver=new ChromeDriver();
驱动程序。获取(“https://www.facebook.com/");
WebElement link=driver.findElement(By.xpath(“//a[contains(text(),\“忘记的帐户?\”)”));
动作动作=新动作(驱动);
操作。按键向下(按键。左键控制)
。单击(元素)
.keyUp(按键。左键控制)
.build()
.perform();
ArrayList选项卡=新建ArrayList(driver.getWindowHandles());
driver.switchTo().window(tab.get(1));
}
WebDriver=new ChromeDriver();
驱动程序。获取(“https://www.facebook.com/");
WebElement link=driver.findElement(By.xpath(“//a[contains(text(),\“忘记的帐户?\”)”));
动作动作=新动作(驱动);
操作。按键向下(按键。左键控制)
。单击(元素)
.keyUp(按键。左键控制)
.build()
.perform();
ArrayList选项卡=新建ArrayList(driver.getWindowHandles());
driver.switchTo().window(tab.get(1));

}

`Actions action=新动作(驱动程序);action.keyDown(Keys.LEFT_控件)。moveToElement(link)。click().keydup(Keys.LEFT_控件)。build().perform();`修改代码这会在新选项卡中打开链接,但该选项卡会立即关闭,链接会在主选项卡中打开。您需要编辑问题以包含所有信息。注释不适用于此。`Actions action=新操作(驱动程序);action.keyDown(Keys.LEFT_控件)。moveToElement(link)。click().keydup(Keys.LEFT_控件)。build().perform();`修改代码这会在新选项卡中打开链接,但该选项卡会立即关闭,链接会在主选项卡中打开。您需要编辑问题以包含所有信息。评论不适合这种情况。