Locating select element in Selenium

2019-09-06 18:34发布

问题:

Below is the combobox:

<TD ALIGN="left" id="oldcontent">
<select name="status" style="width=150" id="newcontentformat"><option value="14" selected="selected">text1</option>
<option value="15">text2</option>
<option value="16">text3</option></select>
</TD>

I need to select text2 in the combobox/dropdown.I have used the below:

selenium.select("//select[starts-with(@name,status)","text2");

The probelm am facing is,its giving me an error text2 not found.Since there are may other select boxes above this the same name status.So i need to select 2nd element of 2 dropdown/combobox.

Please provide me the solution.Its urgent.Thanks in Advance

Another td

<TD align="left" WIDTH="18%"><FONT ID="oldContent">
<select name="status" onchange="selectTime(this.options[this.selectedIndex].value)" id="newcontentformat">
<option value="" selected="selected"></option>
<option value="1">text100</option>
<option value="2">text200<</option>
<option value="3">text300<</option>
</TD>

回答1:

I hope this should work

selenium.select("//td[@id='oldcontent']/select","label=text2")


回答2:

Have you tried with id of select tag.

selenium.select("//select[@id='newcontentformat']","label=text2");

                               or

selenium.select("//td[@id='oldContent']/select[@id='newcontentformat']","label=text2");


回答3:

well. I would try using css selectors solution:

String cssDropdown="select[id='newcontentformat']";
String csstext200="select[id='newcontentformat']>option[value='2']";
driver.findElement(By.cssSelector(cssDropdown)).click();
driver.manage().timeouts().implicitlyWait(1,TimeUnit.SECONDS);
//Thread.sleep(1000);
driver.findElement(By.cssSelector(csstext200)).click();

2nd aproach: if jQuery is supported you can use following:

 JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x=$(\'"+csstext200+"\');");
        stringBuilder.append("return x.click()");
        (String) js.executeScript(stringBuilder.toString());

hope it helps



回答4:

Though this solution is not absolute but it wil work. You can locate this dropdown by using : //select[@name='Status']/following::select[@name='Status'] YOU can add more following depending on the Number of dropdowns. Also you can use preceding it ou want to start backwards. Please go through Selenium.1.0.Testing.Tools.Beginners.Guide Ebbok .



回答5:

WebDriver driver = new InternetExplorerDriver(); driver.get("http://agra.quikr.com/"); driver.findElement(By.xpath("//select[@id='alertcatSelectBox']")).sendKeys("Jobs");

// for your example driver.findElement(By.xpath("//select[@name='status']")).sendKeys("text2");