Selenium webdriver 在表格中搜索一个值,然后单击同一表格另一列中相应的链接
我想在表格中搜索一个特定值,然后单击同一行另一列中相应的链接,下面是我编写的代码。我得到了单元格内容的表格值,现在我需要搜索并单击链接Selenium webdriver 在表格中搜索一个值,然后单击同一表格另一列中相应的链接,selenium-webdriver,Selenium Webdriver,我想在表格中搜索一个特定值,然后单击同一行另一列中相应的链接,下面是我编写的代码。我得到了单元格内容的表格值,现在我需要搜索并单击链接 package checking; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.se
package checking;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
public class TableCorrespondingValueClick {
public static void main(String[] args) {
RemoteWebDriver driver;
//driver = new FirefoxDriver();
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://toolsqa.com/automation-practice-table");
String Searchvalue = "Taiwan";
List<WebElement> heading = driver.findElements(By.xpath(".//*[@id='content']/table/tbody/tr/th"));
List<WebElement> row = driver.findElements(By.xpath(".//*[@id='content']/table/tbody/tr"));
List<WebElement> cell = driver.findElements(By.xpath(".//*[@id='content']/table/tbody/tr/td"));
int rowcount = row.size();
int cellcount = cell.size();
System.out.println("No.of Rows in the Table : "+rowcount);
System.out.println("No.of Cells in the Table : "+cellcount);
for (WebElement c : cell) {
String cellvalue = c.getText();
System.out.println("Cell Values : "+cellvalue);
if (cellvalue.equalsIgnoreCase(Searchvalue)) {
System.out.println("Found");
}
}
driver.quit();
}
}
Can anyone suggest me the solution for this scenario
包装检查;
导入java.util.List;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.remote.RemoteWebDriver;
公共类表对应的值单击{
公共静态void main(字符串[]args){
远程网络驱动程序;
//驱动程序=新的FirefoxDriver();
System.setProperty(“webdriver.chrome.driver”,“/drivers/chromedriver.exe”);
驱动程序=新的ChromeDriver();
driver.manage().window().maximize();
驱动程序。获取(“http://toolsqa.com/automation-practice-table");
字符串Searchvalue=“台湾”;
列表标题=driver.findElements(By.xpath(“./*[@id='content']/table/tbody/tr/th”);
列表行=driver.findElements(By.xpath(“./*[@id='content']/table/tbody/tr”);
列表单元格=driver.findElements(By.xpath(“./*[@id='content']/table/tbody/tr/td”);
int rowcount=row.size();
int cellcount=cell.size();
System.out.println(“表中的行数:“+rowcount”);
System.out.println(“表中的单元格数量:“+cellcount”);
for(WebElement c:cell){
字符串cellvalue=c.getText();
System.out.println(“单元格值:“+cellvalue”);
if(cellvalue.equalsIgnoreCase(Searchvalue)){
System.out.println(“找到”);
}
}
driver.quit();
}
}
有人能为我推荐这个场景的解决方案吗
HTML源
样本表
结构
国家
城市
高度
建造
等级
…
全部的
4栋建筑物
哈利法塔
阿拉伯联合酋长国
迪拜
829米
2010
1.
钟楼酒店
沙特阿拉伯
麦加
601m
2012
2.
台北101
台湾
台北
509m
2004
3.
金融中心
中国
上海
492米
2008
4.
请按照下面的方式操作
public class workingwithTable {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/rajnish/Desktop/Table.html");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
List<WebElement> allvalue = driver.findElements(By.xpath("//*[@class='tsc_table_s13']/tbody/tr"));
// for printing everything in the table
for(int i =0;i<allvalue.size();i++){
System.out.println("Value are : " + allvalue.get(i).getText());
}
// for printing a particular column rows
List<WebElement> allCountrymvalue = driver.findElements(By.xpath("//*[@class='tsc_table_s13']/tbody/tr/td[1]"));
for(int i =0;i<allCountrymvalue.size();i++){
System.out.println("Value are : " + allCountrymvalue.get(i).getText());
}
// for printing all links do it like belwo
List<WebElement> alllinks = driver.findElements(By.xpath("//*[@class='tsc_table_s13']/tbody/tr/td[6]/a"));
for(int i =0;i<alllinks.size();i++){
System.out.println("Link is : " + alllinks.get(i).getText());
}
// Now please note that inside the web-table all column and rows are fixed
// i.e size of each row for each column will be fixed
// i.e if size for Country is = 4 then ,size for City ,height ,build,rank
// and for link will be same i.e =4
// hence on that basic we can do it like below
String MyCountryVal = "Taiwan";
for(int i =0;i<allCountrymvalue.size();i++){
System.out.println("Value are : " + allCountrymvalue.get(i).getText() + "== Corresponding link is : " + alllinks.get(i).getText());
// now on the basis of column value click the corresponding link
if(allCountrymvalue.get(i).getText().equals(MyCountryVal)){
alllinks.get(i).click();
break;
}
}
}
}
public类与表一起工作{
公共静态void main(字符串[]args){
WebDriver=newfirefoxdriver();
驱动程序。获取(“file:///C:/Users/rajnish/Desktop/Table.html");
driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
列出allvalue=driver.findElements(By.xpath(“/*[@class='tsc_table_s13']/tbody/tr”);
//用于打印表中的所有内容
对于(int i=0;i请尝试以下操作:
driver.manage().window().maximize();
driver.get("http://toolsqa.com/automation-practice-table");
Thread.sleep(4000);
String Searchvalue = "Taiwan";
//get name and details link element.
List<WebElement> countryname = driver.findElements(By.cssSelector("tbody>tr>th+td"));
List<WebElement> link = driver.findElements(By.cssSelector("tbody>tr>td>a"));
System.out.println("No.of Rows in the Table : "+countryname.size());
System.out.println("No.of link in the Table : "+link.size());
for (int i =0; i<countryname.size();i++) {
String country = countryname.get(i).getText();
System.out.println("country Values : "+country);
if (country.equalsIgnoreCase(Searchvalue)) {
System.out.println("Found");
link.get(i).click();
break;
}
}
driver.manage().window().maximize();
驱动程序。获取(“http://toolsqa.com/automation-practice-table");
睡眠(4000);
字符串Searchvalue=“台湾”;
//获取名称和详细信息链接元素。
List countryname=driver.findElements(By.cssSelector(“tbody>tr>th+td”);
列表链接=driver.findElements(由.cssSelector(“tbody>tr>td>a”);
System.out.println(“表中的行数:“+countryname.size()”);
System.out.println(“表中的链接数量:“+link.size()”);
对于(int i=0;iplz粘贴sameu的html源代码要单击详细信息按钮吗?嗨,如果可能,请复制粘贴实际源代码,而不是img,如前所述,给出我想要验证的任何答案it@noor是的,我想点击details@raj我已经粘贴了HTML代码,感谢它的工作。我们可以通过传递表定位器来简化吗或者仅仅通过传递一个定位器是的,我只是做了很长的一段路来让您了解webtable中的一切是如何工作的
driver.manage().window().maximize();
driver.get("http://toolsqa.com/automation-practice-table");
Thread.sleep(4000);
String Searchvalue = "Taiwan";
//get name and details link element.
List<WebElement> countryname = driver.findElements(By.cssSelector("tbody>tr>th+td"));
List<WebElement> link = driver.findElements(By.cssSelector("tbody>tr>td>a"));
System.out.println("No.of Rows in the Table : "+countryname.size());
System.out.println("No.of link in the Table : "+link.size());
for (int i =0; i<countryname.size();i++) {
String country = countryname.get(i).getText();
System.out.println("country Values : "+country);
if (country.equalsIgnoreCase(Searchvalue)) {
System.out.println("Found");
link.get(i).click();
break;
}
}