在我的网页中,我有一个html表,它包含多个单选按钮。我想选择一个单选按钮。到目前为止,我能够从表中找到值,但无法选择。这是我的代码:我在语法aname.click()上遇到错误; 错误是“方法click()未定义类型String”
import java.io.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class SendTxn1 {
static WebDriver d1=null;
public static void main(String[] args) throws IOException, InterruptedException
{
File file1=new File("C:\\Selenium\\IEDriverServer_Win32_2.35.3\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver",file1.getAbsolutePath());
d1= new InternetExplorerDriver();
d1.get("http://10.00.00.107/");
WebElement table_element = d1.findElement(By.id("tblSendMoneyPayoutAgents"));
List<WebElement> tbl_rows=table_element.findElements(By.xpath("id('tblSendMoneyPayoutAgents')/tbody/tr"));
System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tbl_rows.size());
int row_num,col_num;
row_num=1;
col_num=1;
String aname;
for(WebElement trElement : tbl_rows)
{
List<WebElement> tbl_col=trElement.findElements(By.xpath("td"));
for(WebElement tdElement : tbl_col)
{
aname = tdElement.getText();
if(aname.equals("VNM - VN Shop Herat"))
aname.click()l
break;
System.out.println(aname);
col_num++;
}
row_num++;
}
}
}
答案 0 :(得分:0)
我认为您需要使用executeScript
来选择单选按钮。此方法执行javascript字符串。把它想象为eval
,但是来自Selenium。根据您使用的Selenium版本,您可以将您的webdriver转换为JavascriptExecutor
,然后拨打executeScript
,例如
((JavascriptExecutor) d1).executeScript("alert('replace the alert with the code to select your radio button');");
修改强>
如果您不想使用executeScript
,则需要获取与您的单选按钮对应的WebElement
,然后调用其click
方法。在你的情况下,你试图点击getText
返回的字符串,因此你的错误:)。因此,您缺少一个从表格单元格中选择单选按钮的步骤。
某些事情(xpath查询可能是错误的)
List<WebElement> radio_buttons = tdElement.findElements(By.xpath('input[type=radio]'));
编辑2
要以复制粘贴形式为您拼出答案,请替换
aname = tdElement.getText();
if(aname.equals("VNM - VN Shop Herat"))
aname.click();
与
if(tdElement.getText().equals("VNM - VN Shop Herat")) {
List<WebElement> radio_buttons = tdElement.findElements(By.xpath('input[type=radio]'));
for(WebElement radio : radio_buttons) {
//now check which radio you want to click
}
}