【问题标题】:Selenium Web driver select data from drop down by reading drop down value from excel sheetSelenium Web 驱动程序通过从 excel 表中读取下拉值来从下拉列表中选择数据
【发布时间】:2016-07-27 18:19:09
【问题描述】:

我正在尝试通过从 excel 中读取下拉值来选择下拉值。我尝试了以下代码,但是它没有根据 excel 表中提到的数据选择值。对我来说,除了“性别”下拉列表之外,所有数据都在相应字段中正确填充。

以下是 HTML 代码和 UI 的屏幕截图:

以下是我的 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>

我在下面的 excel 表中填充了测试数据,其中“女性”是下拉值:

UserName password123    100000005   Elena   Sawyerehde  Female

以下是我的代码:

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

            }
            }
}

【问题讨论】:

  • 输入类型为文本。你确定它是一个选择元素吗?它还具有只读属性为真,所以不确定你是否可以更新任何东西......
  • 我可以将发送键用于输入类型,但我还想通过从 Excel 表中读取来使用下拉值。所以它既可以在字段中输入值,也可以从下拉列表中选择值。
  • 嗨,好奇,“女性”这个值什么时候出现。我猜在单击下拉列表时它会显示值“女性”。我说的对吗??
  • 是的,当我点击下拉菜单时,会出现女性选项。并且男性选项默认显示选择选项。

标签: java selenium-webdriver testng jxl


【解决方案1】:

由于您粘贴的代码中没有 Select 标记,因此使用 select 单击下拉菜单将无济于事。

根据您的测试脚本和 Html 代码,下拉列表的 id 值不同。

//*[@id='widget___o3id7'] - 在 xpath 中用于选择下拉菜单,其中在 html 代码中为 id = "__o3id7"

修改后试试

最好分享你的 UI 以了解场景并得出结论


感谢分享 UI 和 HTML 代码。

    driver.findElement(By.id("__o3id7")).click();
    Thread.sleep(1500);
    driver.findElement(By.xpath("//*[contains(text(),'Female')]")).click();

替换您的 select 语句并尝试此代码。希望这会从您的下拉菜单中点击“女性”。

【讨论】:

  • 嗨 Anand - 我按照你的建议更新了 id,我还在屏幕截图中提供了 UI 屏幕截图和 HTML 和 Xpath 信息。
  • @Curious 我的回答对你有帮助吗
  • 不,Anand 你的回答没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-03
  • 1970-01-01
  • 2022-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多