Java 使用firefox webdriver尝试从下拉列表加载选项名,但不起作用?

Java 使用firefox webdriver尝试从下拉列表加载选项名,但不起作用?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,下面我无法使用名称顺序加载手机列表,使用代码它正在从下拉名称中选择选项,但立即仅加载上一页。当我运行下面的代码时,它正在选择名称标签,但结果没有显示在输出屏幕上 package selflearning; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.Fir

下面我无法使用名称顺序加载手机列表,使用代码它正在从下拉名称中选择选项,但立即仅加载上一页。当我运行下面的代码时,它正在选择名称标签,但结果没有显示在输出屏幕上

package selflearning;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Guru99Ecommerce1 {

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

        System.setProperty("webdriver.gecko.driver","C:\\geckodriver\\geckodriver.exe"); 
        WebDriver driver=new FirefoxDriver();
        driver.get("http://live.guru99.com/index.php/");

        String title=driver.getTitle();
        String expectedTitle="Home page";
        System.out.println("The title of the webPage is "+title);
        expectedTitle.equalsIgnoreCase(title);
        System.out.println("Title is verified");
        driver.findElement(By.xpath("//a[text()='Mobile']")).click();

        String nextTitle=driver.getTitle();
        System.out.println("The title of next page"+nextTitle);

        String nextExpectedTitle="pageMobile";
        nextExpectedTitle.equalsIgnoreCase(nextTitle);
        System.out.println("The next title is verified");


        driver.findElement(By.xpath("//div[@class='category-products']//div/div[@class='sorter']/div/select[@title='Sort By']")).click();
        driver.findElement(By.xpath("//div[@class='category-products']//div/div[@class='sorter']/div/select/option[2]")).click();


        Thread.sleep(5000);




    }

}

尝试此代码,您将错过
Select
classtoselect下拉列表

   driver.get("http://live.guru99.com/index.php/");

        String title=driver.getTitle();
        String expectedTitle="Home page";
        System.out.println("The title of the webPage is "+title);
        expectedTitle.equalsIgnoreCase(title);
        System.out.println("Title is verified");
        driver.findElement(By.xpath("//a[text()='Mobile']")).click();

        String nextTitle=driver.getTitle();
        System.out.println("The title of next page"+nextTitle);

        String nextExpectedTitle="pageMobile";
        nextExpectedTitle.equalsIgnoreCase(nextTitle);
        System.out.println("The next title is verified");

        Select s=new Select(driver.findElement(By.xpath(".//*[@id='top']/body/div[1]/div/div[2]/div/div[2]/div[1]/div[3]/div[1]/div[1]/div/select")));
        s.selectByVisibleText("Name");

实际上对于HTML选择标签,我们可以直接点击您想要的选项,无需点击选择先展开选项列表,然后点击选项

因此,您可以尝试以下代码:

public void chooseOrderBy(String orderBy) {

driver
.findElement(by.css("[title='Sort By']"))
.findElement(by.xpath(String.format("./option[contains(text(), '%s')]", orderBy))
.click();

}
从用户体验来看,用户需要先查看所有选项,然后才能选择所需的选项,因此用户必须单击选择按钮,使其首先弹出所有选项

但自动化在选择一个选项之前不需要查看所有选项。可直接点击该选项完成选择

运行上述代码时,您会注意到在自动运行期间,在选择选项之前会出现选择不弹出选项

当然,点击选择弹出选项,然后选择选项,这也应该有效。但还有一次在页面上操作


提醒:直接点击option only support dropdown list by HTML Select tag,对于那些Jquery下拉列表,你必须点击弹出选项,然后从中选择一个。

你到底想打开上面提到的URL,然后转到移动部分,默认情况下,它们按位置顺序排序,我想以名称顺序显示它们尝试这个它正在工作选择它工作的类man,我试图找到Name tag的xpath,然后单击它,了解它是如何工作的。但是需要问一件事,在按Name tag排序之后,比如说,如果我想检查显示的项目是否按升序排列,那么我如何才能做到这一点?我的代码适用于u@gaurav。如果是,请投赞成票并接受为反对票