Selenium webdriver Selenium webdriver java下拉列表问题“;元素应该是;选择";但是是",;分区&引用;

Selenium webdriver Selenium webdriver java下拉列表问题“;元素应该是;选择";但是是",;分区&引用;,selenium-webdriver,element,Selenium Webdriver,Element,当尝试在Amazon站点中单击注销时,会显示错误消息“Element本应为“select”,但为“div”。在代码末尾,CSS选择器和XPATh标识符中的任何一个都不起作用。请帮助解决此问题 import java.sql.Driver; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.ope

当尝试在Amazon站点中单击注销时,会显示错误消息“Element本应为“select”,但为“div”。在代码末尾,CSS选择器和XPATh标识符中的任何一个都不起作用。请帮助解决此问题

import java.sql.Driver;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class FirstClass {

    private static final String SeleniumWait = null;

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Documents\\Selenium\\chromedriver_win32\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        driver.get("https://www.amazon.com/");
        driver.findElement(By.id("nav-link-accountList")).click();
        driver.findElement(By.id("ap_email")).sendKeys("welcom@gmail.com");
        driver.findElement(By.id("ap_password")).sendKeys("Password");
        driver.findElement(By.id("signInSubmit")).click();
        driver.findElement(By.xpath("//*[@id='nav-link-accountList']/span[1]")).click();
        //driver.findElement(By.xpath("//*[@id='nav-al-container']")).click();
        JavascriptExecutor jse = (JavascriptExecutor)driver;
        jse.executeScript("scroll(0, 50);"); 

        Select dropdown = new Select(driver.findElement(By.xpath("//*[@id='nav-al-container']")));
        dropdown.selectByVisibleText("Not test? Sign Out");
        WebDriverWait wait = new WebDriverWait(driver, 40);// 1 minute 
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("//*[@id='nav-item-signout']/span")));
        driver.findElement(By.cssSelector("#nav-item-signout > span")).click();//CSS selector

        //driver.findElement(By.xpath("//*[@id='nav-item-signout']/span")).click(); //XPATH 
在以下行中:

Select dropdown = new Select(driver.findElement(By.xpath("//*[@id='nav-al-container']")));
它只适用于带有
html标记的下拉列表,但在html中它属于
标记。 您需要用不同的方法处理此下拉列表

  • 您需要通过.xpath(“/*[@id='nav-link-accountList']”)将鼠标悬停在
    上,而不是使用
    操作来单击它
  • 动作动作=新动作(驱动); action.moveToElement(By.xpath(“/*[@id='nav-link-accountList']); action.build().perform();
  • 然后,您需要单击位于by.id(“导航项目签出”)的元素进行签出
  • Actions action = new Actions(driver); action.moveToElement(By.xpath("//*[@id='nav-link-accountList']")); action.build().perform();