如何使用按钮的标签文本单击按钮

时间:2016-11-18 10:48:50

标签: selenium-webdriver

有人可以帮我解决以下事项吗?

我想点击一个包含“恢复扫描”文本的按钮。 在下面的脚本中。而不是硬编码(Resume Scanning),我如何从属性文件中传递值?

driver.findElement(by.xpath("//button[contains(.,'Resume Scanning')]"));

谢谢, Kannan C

4 个答案:

答案 0 :(得分:1)

尝试如下:

String.format

或使用String string = String.format("//button[contains(.,'%s')]", label); driver.findElement(By.xpath(string));

//Set up an event
$(".js__scroll__canvas").on('datachange', function(e, key){
     alert( "Handler for .datachange() called." );
});

// Then trigger it when you data gets updated 
$(".js__scroll__canvas").data('posx', 123).trigger('datachange');

参考:

  1. Java - Including variables within strings?

答案 1 :(得分:0)

您可以使用以下java代码从属性文件中读取

FileInputStream in = new FileInputStream("location of properties file");
Properties prop = new Properties();
prop.load(in);
String buttonText=prop.getProperty(propertyName);
driver.findElement(By.xpath(".//button[contains(text(),'"+buttonText+"')]"));

答案 2 :(得分:0)

您可以使用属性文件来读取文本值。这是一个例子:

inputParams.properties
----------------------
buttonLabel=Resume Scanning

使用以下示例代码从属性文件中读取数据:

FileReader reader=new FileReader("inputParams.properties");  
Properties p=new Properties();  
p.load(reader);  
System.out.println(p.getProperty("buttonLabel"));

希望它有所帮助。

答案 3 :(得分:0)

By.xpath(“ // button [包含(。,'添加策略')]”)

By.xpath(“ // button [包含(。,'提交”)]“)

相关问题