[英]Selenium Web driver select data from drop down by reading drop down value from excel sheet
I am trying to select drop down value by reading drop down value from excel. 我试图通过从excel中读取下拉值来选择下拉值。 I tried following code however, it is not selecting value as per data mentioned in the excel sheet. 我尝试了以下代码,但是,它没有按照excel工作表中提到的数据选择值。 For me all data are populating correctly in the respective field except Gender drop down. 对我来说,所有数据均在“性别”下拉列表中正确填充。
following are Screen shot of HTML code and UI: 以下是HTML代码和UI的屏幕截图:
Following are my HTML code: 以下是我的HTML代码:
<td class="codetable last-cell" headers="N224AA-4-2">
<div id="widget___o3id7" class="dijit dijitReset dijitInline dijitLeft codetable dijitTextBox dijitComboBox" lang="en-US" role="listbox" dir="ltr" widgetid="__o3id7" aria-expanded="false">
<div class="dijitReset dijitRight dijitButtonNode dijitArrowButton dijitDownArrowButton dijitArrowButtonContainer" role="presentation" data-dojo-attach-point="_buttonNode, _popupStateNode" popupactive="true">
<input class="dijitReset dijitInputField dijitArrowButtonInner" type="text" role="presentation" readonly="readonly" tabindex="-1" value="▼ ">
</div>
<div class="dijitReset dijitValidationContainer">
<input class="dijitReset dijitInputField dijitValidationIcon dijitValidationInner" type="text" role="presentation" readonly="readonly" tabindex="-1" value="Χ ">
</div>
<div class="dijitReset dijitInputField dijitInputContainer">
<input id="__o3id7" class="dijitReset dijitInputInner" type="text" aria-haspopup="true" role="textbox" data-dojo-attach-point="textbox,focusNode" autocomplete="off" aria-required="true" tabindex="0" title="Gender Mandatory" size="1" value="Male" aria-owns="__o3id7_popup" aria-activedescendant="__o3id7_popup1">
<input type="hidden" name="__o3id7" value="SX1">
</div>
</div>
</td>
I populated test data in the excel sheet as below where "Female" is drop down value: 我在excel表中填充了以下“女性”下拉值的测试数据:
UserName password123 100000005 Elena Sawyerehde Female
Following are my code: 以下是我的代码:
package com.access;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.*;
import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
public class Registration {
static WebDriver driver;
@BeforeMethod
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Directory\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
@Test
public void TestCase1() throws Exception {
FileInputStream fi=new FileInputStream("C:\\File\\Book2.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
driver.get("https://example.com");
try
{
for (int i = 0; i < s.getRows(); i++)
{
//Read data from excel sheet
String s1 = s.getCell(0,i).getContents();
String s2 = s.getCell(1,i).getContents();
String s3 = s.getCell(2,i).getContents();
String s4 = s.getCell(3,i).getContents();
String s5 = s.getCell(4,i).getContents();
String s6 = s.getCell(5,i).getContents();
driver.findElement(By.xpath("html/body/div[2]/form/input[1]")).sendKeys(s1);
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[2]/form/input[2]")).sendKeys(s2);
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[2]/a/span/span/span")).click();
Thread.sleep(2000);
Assert.assertEquals("Testing Hub", driver.findElement(By.xpath("//*[@id='app-banner']/div[1]/div/h2")).getText());
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[1]/div[4]/div[1]/div[4]/div/div[2]/div/div/div/span[1]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[1]/div[4]/div[3]/div[2]/div[3]/div[3]/div[1]/div/div[2]/div/div/div/span/span/span/span[2]")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("html/body/div[4]/table/tbody/tr[2]/td[2]")).click();
Thread.sleep(2000);
driver.switchTo().frame("iframe-curam_ModalDialog_0");
Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='__o3id0']")).sendKeys(s3);
Thread.sleep(1000);
driver.findElement(By.xpath("html/body/div[3]/form/div/div[5]/a[1]/span/span/span")).click();
Thread.sleep(1000);
Assert.assertEquals("There are no matching items based on the Search Criteria entered.", driver.findElement(By.xpath("html/body/div[3]/div/ul/li/div")).getText());
Thread.sleep(1000);
driver.findElement(By.xpath("html/body/div[4]/div[2]/a/span/span/span")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[3]/form/div/div[2]/div/table/tbody/tr[1]/td[1]/input")).sendKeys(s4);
Thread.sleep(1000);
driver.findElement(By.xpath("html/body/div[3]/form/div/div[2]/div/table/tbody/tr[2]/td[1]/input")).sendKeys(s5);
Thread.sleep(1000);
new Select(driver.findElement(By.id("___o3id7"))).selectByValue(s6);
Thread.sleep(2000);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
As there is no Select tag in the code you pasted, using select
for clicking dropdown won't help. 由于您粘贴的代码中没有“选择”标签,因此使用“ select
”单击下拉列表将无济于事。
As per your test script and Html code the id
value for the dropdown is different. 根据您的测试脚本和HTML代码,下拉列表的id
值是不同的。
//*[@id='widget___o3id7']
- used in the xpath to select dropdown, where in the html code id = "__o3id7"
. //*[@id='widget___o3id7']
-在xpath中用于选择下拉菜单,其中html代码id = "__o3id7"
。
Try after changing them 更改后尝试
Its better to share your UI to understand the scenario and come to a conclusion 最好共享您的UI以了解场景并得出结论
Thanks for sharing the UI and HTML code. 感谢您分享UI和HTML代码。
driver.findElement(By.id("__o3id7")).click();
Thread.sleep(1500);
driver.findElement(By.xpath("//*[contains(text(),'Female')]")).click();
Replace your select
statement and try this codes. 替换您的select
语句,然后尝试使用此代码。 Lets hope this would click 'Female' from your drop down. 希望这会从您的下拉列表中单击“女性”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.