0
我想創建一個公共類來點擊一個網頁上的一個項目,只需將它傳遞給我使用的xpath和驅動程序。我希望能夠只是做:添加一個公共類來點擊Java使用xpath Selenium
ClickByXpath(driver, "/html/body/div/div[3]/form/div[2]/div[2]/div[1]/div[1]/div[3]/div/div[3]/div/input[1]");
下面是我使用的代碼,但它的抱怨,該方法XPath字符串是不適用:
package TestPackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Question {
public static void main(String[] args) throws Exception {
// The Firefox driver supports javascript
WebDriver driver = new FirefoxDriver();
// Go to google
driver.get("http://www.google.com");
//click in the searchbox
ClickByXpath(driver, "/html/body/div/div[3]/form/div[2]/div[2]/div[1]/div[1]/div[3]/div/div[3]/div/input[1]");
}
public static void ClickByXpath(WebDriver [] driverUsed , String[] XPath_to_click) throws Exception {
driverUsed.findElement(By.xpath(XPath_to_click)).click();
}
}
只是想知道爲什麼你會傳遞這兩種類型的數組,而一次只能使用一個實例? – Saifur
感謝您的回覆。我只是從這開始,所以無疑是做錯了。他們是陣列嗎,因爲我用方括號? – Andrew