2016-11-18 40 views
1

有人可以幫我在下面?如何點擊一個按鈕使用按鈕的標籤文字

我想點擊一個按鈕誰的文本「繼續掃描」。 在下面的腳本中。而不是硬編碼(簡歷掃描),我怎麼能從屬性文件傳遞值?

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

感謝, 卡納安Ç

回答

1

嘗試如下:

String label = "Resume Scanning"; 
driver.findElement(By.xpath("//button[contains(.,'" + label + "')]")); 

或者使用String.format

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

參考:

  1. Java - Including variables within strings?
+1

非常感謝。它解決了 –

+0

我用這個String label =「Resume Scanning」; driver.findElement(By.xpath(「// button [contains(。,'」+ label +「')]」)); –

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+"')]")); 
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")); 

希望它能幫助。

+0

謝謝Karthik –

相關問題