![](/img/trans.png)
[英]How to click on an image/link using Selenium WebDriver (Java)
[英]How to click on an image using selenium WebDriver
我是硒的新手,無法單擊所附的屏幕快照中突出顯示的名為“ Register”的圖像。誰能讓我知道為什么在運行時未識別Web元素以及如何識別並單擊它?
import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.annotations.Test;
public class Registration {
public WebDriver driver1;
public String exepath="C:\\Users\\ADMIN\\Downloads\\chromedriver.exe";
public String filepath="C:\\Users\\ADMIN\\Desktop\\Book1tetsts.xls";
public FileInputStream file;
public String userID;
public String password;
public String Fname;
public String Lname;
private Object wait;
@Test
void formfilling() throws BiffException, IOException, InterruptedException
{
file = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(file);
Sheet sh = wb.getSheet(0); // this is to get the access to Sheet1.
userID= sh.getCell(0,0).getContents();
password= sh.getCell(1,0).getContents();
Fname= sh.getCell(2,0).getContents();
Lname=sh.getCell(3,0).getContents();
System.setProperty("webdriver.chrome.driver", exepath);
driver1= new ChromeDriver();
driver1.get("http://www.esevaonline.telangana.gov.in");
synchronized (driver1) {
driver1.wait(15000);
}
driver1.findElement(By.xpath("//*[@id='lhsNav']/a/img[@src='images/register2.gif']")).click();
//UserID
driver1.findElement(By.xpath("/html/body/center/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[1]/td[2]/input")).sendKeys(userID);
//Password
driver1.findElement(By.xpath("/html/body/center/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[2]/td[2]/input")).sendKeys(password);
//Re-Type Password
driver1.findElement(By.xpath("/html/body/center/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[3]/td[2]/input")).sendKeys(password);
driver1.findElement(By.xpath("/html/body/center/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[5]/td[2]/input")).sendKeys(Fname);
//LastName
driver1.findElement(By.xpath("/html/body/center/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[6]/td[2]/input")).sendKeys(Lname);
//DOB
}
}
給定的“ Register” img在frame內部。 因此,您需要先切換框架,然后在該框架內的Webelement上執行任何事件。
請在您的代碼中添加以下代碼行。
driver1.switchTo().frame("mainFrame"); // switch frame
driver1.findElement(By.xpath("//*[@id='lhsNav']/a/img[@src='images/register2.gif']")).click();
//UserID
// other operation
希望這對您有所幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.