首页 > 解决方案 > 为什么 Selenium 测试网站不能在多个页面上工作?

问题描述

代码工作的第一部分将起作用。但是第二部分不行,也没有出现错误,也不知道问题出在哪里。所以请帮忙。

第一部分是登录页面,第二部分是主页。

package Test;

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.chrome.ChromeDriver;

public class test {

  public static void main(String[] args) throws InterruptedException {

    WebDriver driver;

    System.setProperty("webdriver.gecko.driver",
                        "E:\\Quality\\drivers\\geckodriver.exe");

    driver =new FirefoxDriver();

    driver.get("https://www.linkedin.com/uas/login");
        // first part//
    driver.findElement(By.xpath("//*[@id=\"session_key-login\"]")).click(); 

    driver.findElement(By.xpath("//[@id=\"session_keylogin\"]")).sendKeys("Email");

    driver.findElement(By.xpath("//*[@id=\"session_password-login\"]")).click();
    driver.findElement(By.xpath("//*[@id=\"session_password-login\"]")).sendKeys("*******");

    driver.findElement(By.xpath("//*[@id=\"btn-primary\"]")).click();
    // second part//
    WebElement test = null ;
    test.findElement(By.xpath("/html/body/div[5]/div[5]/aside/div/header")).click();

  }
}  

标签: javaeclipseselenium

解决方案


xpath您为电子邮件文本框输入了错误的 ID 。

您应该使用session_key-login而不是session_keylogin.

只需在您使用sendKeys()方法的地方使用以下修改后的代码:

driver.findElement(By.xpath("//*[@id=\"session_key-login\"]")).sendKeys("Email");

第二部分解决方案

跳过 WebElement 声明,因此注释该行 //WebElement test = null;

使用driver对象的线

driver.findElement(By.xpath("/html/body/div[5]/div[5]/aside/div/header")).click();

您也可以使用 xpath //*[@id=\"msg-overlay\"]/div/header
,因此修改后的代码是:

driver.findElement(By.xpath("//*[@id=\"msg-overlay\"]/div/header")).click();

推荐阅读