Java 通过selenium在表中选择动态选项

Java 通过selenium在表中选择动态选项,java,selenium,testing,automation,Java,Selenium,Testing,Automation,我的测试站点的登录工作流如下: 使用用户名和密码登录 在项目列表中选择一个项目并提交 处理所选项目 转到“订阅项目”页面 订阅另一个项目并保存 项目列表根据用户的订阅按字母顺序排序。 在我订阅另一个选项之前,我的Selenium代码如下所示。我所需选项(选项3)的顺序随字母顺序的变化而变化 package mypackage; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.open

我的测试站点的登录工作流如下:

  • 使用用户名和密码登录
  • 在项目列表中选择一个项目并提交
  • 处理所选项目
  • 转到“订阅项目”页面
  • 订阅另一个项目并保存
  • 项目列表根据用户的订阅按字母顺序排序。 在我订阅另一个选项之前,我的Selenium代码如下所示。我所需选项(选项3)的顺序随字母顺序的变化而变化

    package mypackage;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class MyClass {
    
        public static void main(String[] args) {
            WebDriver driver = new FirefoxDriver();
            driver.manage().window().maximize();
            //To wait until the element get visible or invisible.
            WebDriverWait wait = new WebDriverWait(driver, 25);
            driver.get("https://arunelias.in/testing/randomoption.php");  
            //For wait until the element get visible.
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("gvItemListing_grdSelect_0")));
            //Select Option 3 displayed and submit
            driver.findElement(By.id("gvItemListing_grdSelect_3")).click();
            driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
        }
    
    }
    
    我创建了一个模拟项目列表的示例页面

    • 我想从表中选择一个选项(比如选项3)
    • 我如何才能找到所需选项显示在哪一行
    • 如何选择“选项3”旁边显示的单选选项

    下面是一个完整的示例,说明如何根据单选选项旁边的文本选择单选选项

    driver.get("https://arunelias.in/testing/randomoption.php");    
    
    
    WebElement allItems = driver.findElement(By.id("gvItemListing"));
    
    
    List<WebElement> tableId = allItems.findElements(By.tagName("td"));
    
    
    String optionString = "Option 3"; 
    
    for(int i = 1 ; i< tableId.size() ; i=i+2){
    
        if(tableId.get(i).getText().equalsIgnoreCase(optionString) ){
            tableId.get(i-1).click();
            break;
        }
    
    }
    

    希望它能解决您的问题。

    要单击文本为
    选项3的
    单选按钮,您可以使用以下代码块:

    driver.get("https://arunelias.in/testing/randomoption.php");    
    List<WebElement> options = driver.findElements(By.xpath("//table[@id='gvItemListing']/tbody/tr//td[.='Option']"));
    for(WebElement option:options)
        if(option.getAttribute("innerHTML").equalsIgnoreCase("Option 3") )
            option.findElement(By.xpath("//self//preceding::input[2]")).click();
    
    driver.get(“https://arunelias.in/testing/randomoption.php");    
    List options=driver.findElements(By.xpath(//table[@id='gvItemListing']/tbody/tr//td[.='Option']);
    用于(WebElement选项:选项)
    if(option.getAttribute(“innerHTML”).equalsIgnoreCase(“option 3”))
    option.findElement(By.xpath(“//self//preference::input[2]”)。单击();
    
    (欢迎来到SO!)
    我如何才能[做点什么]
    我觉得自己“在语法上受到了挑战”(缺乏理解这一点的智慧)。你能试着用简单的句子来表达吗?@Arun我已经为你的问题添加了解决方案。请检查此项并让我知道您的反馈谢谢@mahmud riad,它起作用了。单击包含单选选项的整个
    非常棒!我不知道它能在所有浏览器上运行,但它能在Firefox上运行。@Arun很高兴收到你的来信。如果可行,那么接受答案并投票表决。所以其他人可以从这里得到帮助谢谢@DebanjanB,不幸的是这不起作用。通过将第一个xpath更改为
    //table[@id='gvItemListing']/tbody/tr/td
    ,我可以导航到正确的选项
    选项3
    。但是通过
    click()。检查我的更新答案并让我知道状态。问题在于相对xpath
    //self
    。通过将代码更改为
    //previous::input[1]
    ,我可以使代码正常工作。我也建议编辑。@Arun很好,但我看你不喜欢这样:)
    driver.get("https://arunelias.in/testing/randomoption.php");    
    List<WebElement> options = driver.findElements(By.xpath("//table[@id='gvItemListing']/tbody/tr//td[.='Option']"));
    for(WebElement option:options)
        if(option.getAttribute("innerHTML").equalsIgnoreCase("Option 3") )
            option.findElement(By.xpath("//self//preceding::input[2]")).click();