【问题标题】:get text from Excel data and click on web element Selenium radio button从 Excel 数据中获取文本并单击 Web 元素 Selenium 单选按钮
【发布时间】:2019-03-30 10:14:11
【问题描述】:

两个测试单独工作都很好我需要帮助将它们合并。
我无法同时测试两者。
如果工作表单元格被读取为男性,则应单击男性单选按钮。 请帮我解决这个测试 ExcelRead33.java 将从 Excel 表中读取特定单元格。 facebookradio.java 将在单选按钮上运行测试。 用于数据的 Excel 文件。

package testpoi;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;




public class facebookradio {


public static void main(String[] args) throws Exception {

WebDriver driver=new ChromeDriver();

driver.manage().window().maximize();

driver.get("http://www.facebook.com");

WebElement male_radio_button=driver.findElement(By.id("u_0_a"));

male_radio_button.click();


WebElement female_radio_button=driver.findElement(By.id("u_0_9"));

female_radio_button.click();

ExcelRead33.java 将从 Excel 表中读取特定单元格。

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    //How to read excel files using Apache POI
    public class ExcelRead33 {
    public static void main (String [] args) throws IOException{

    FileInputStream fis = new FileInputStream("C:/DDFREAD.xlsx");
    XSSFWorkbook workbook = new XSSFWorkbook(fis);
    XSSFSheet sheet = workbook.getSheetAt(1);

    //Read specific row and column 
    Row row = sheet.getRow(2);
    Cell cell = row.getCell(8);
    //This will print data which I want to click 
    System.out.println(cell);

Excel File

【问题讨论】:

  • 我认为您的意思是“无法”而不是“不可用”。请在原始帖子中显示您的尝试以及您的代码清单以及您尝试时遇到的错误。

标签: java selenium selenium-webdriver radio-button apache-poi


【解决方案1】:

在学习 Selenium 之前提高您的核心 Java 技能。这是一个肮脏的代码 [没有利用 OOPs] 你正在尝试执行的操作。

FileInputStream fis = new FileInputStream(System.getProperty("user.dir") + "/src/october2018/Q52995381.xlsx");
    XSSFWorkbook workbook = new XSSFWorkbook(fis);
    XSSFSheet sheet = workbook.getSheet("Sheet1");

    // Read specific row and column
    Row row = sheet.getRow(2);
    Cell cell = row.getCell(7);
    // This will print data which I want to click
    System.out.println(cell);

    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/src/drivers/chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("http://www.facebook.com");
    if (cell.toString().contentEquals("male")) {
        WebElement male_radio_button = driver.findElement(By.xpath("//input[@value='2']"));
        male_radio_button.click();
    } else {
        WebElement female_radio_button = driver.findElement(By.xpath("//input[@value='1']"));
        female_radio_button.click();
    }

【讨论】:

    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 2017-03-25
    相关资源
    最近更新 更多