简体   繁体   English

从硒Webdriver的下拉菜单中选择值的问题

[英]Issues in selecting value from dropdown in selenium webdriver

How to select value from dropdown if ID of selector is repeat in signup page? 如果在注册页面重复选择器的ID,如何从下拉列表中选择值? I an writing test script in selenium web driver to retrieve the value from drop down control but always element not find exception is showing. 我在Selenium Web驱动程序中编写测试脚本以从下拉控件中检索值,但始终显示元素未找到异常。

The code is, 代码是

Select dropdown = new Select(driver.findElement(By.id("cat_id"))); 
dropdown.selectByIndex(1);

The page targeted is : http://talentrack.in/register 目标页面是: http : //talentrack.in/register

Simple solution to your problem is using css, or xpath selectors: 解决问题的简单方法是使用CSS或xpath选择器:

Select dropdown = new Select(driver.findElement(By.cssSelector("#cat_id.req"))); 
dropdown.selectByIndex(1);

Before trying to write more code I highly recommend you learn how to use css and xpath selectors. 在尝试编写更多代码之前,我强烈建议您学习如何使用css和xpath选择器。 Great place to start is here . 这里是一个很好的起点。

You should also consider installing FirePath Firefox plug-in, since it's a great tool to boost your learning speed. 您还应该考虑安装FirePath Firefox插件,因为它是提高学习速度的好工具。

Edit: 编辑:

Here is my full code: 这是我的完整代码:

import org.junit.Test;
import org.openqa.selenium.By;
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 test{

private WebDriver driver;
private WebDriverWait wait;

    @Test
    public void main() throws InterruptedException{     
    driver = new ChromeDriver();
    wait = new WebDriverWait(driver, 30);
    driver.manage().window().maximize(); 
    driver.get("http://talentrack.in/register");
    wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#cat_id.req")));
    Select dropdown = new Select(driver.findElement(By.cssSelector("#cat_id.req"))); 
    dropdown.selectByIndex(5);
    Thread.sleep(5000);   
    driver.close();
    }}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM