使用firefox webdriver尝试从下拉列表中加载选项名称,但不起作用? [英] Using firefox webdriver trying to load the option Name from dropdown but not working?

查看:96
本文介绍了使用firefox webdriver尝试从下拉列表中加载选项名称,但不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

below i am not able to load the list of mobiles using Name order, using code it is selecting the option from drop-down NAME but the instantly loads the previous page only. when i run the below code it is selecting the Name tag but result is not showing on the output screen.

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类以选择下拉列表

Try this code, You miss the Select class to select drop down

   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");

这篇关于使用firefox webdriver尝试从下拉列表中加载选项名称,但不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆