gpt4 book ai didi

java - 通过 Selenium 在表中选择动态选项

转载 作者:行者123 更新时间:2023-11-28 20:13:48 25 4
gpt4 key购买 nike

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

  1. Login with Username and Password
  2. Select an Item in the Item list and Submit
  3. Work on the selected Item
  4. Go to Subscribe Item page
  5. Subscribe to another Item and save

项目列表根据用户的订阅按字母顺序排序。我的 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();
}

}

我创建了一个示例页面 https://arunelias.in/testing/randomoption.php模仿项目列表。

  • I want to select an option (say Option 3) from the table.
  • How do I will be able to find in which Row my required Option is displayed?
  • How do I will be able to select the radio option displayed beside "Option 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 的单选按钮。如果您想检查任何其他选项,只需将选项名称放入

String optionString = "Your option name";

希望能解决您的问题。

关于java - 通过 Selenium 在表中选择动态选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47847598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com