如何使用selenium WebDriver单击图像

时间:2017-12-30 16:46:25

标签: java selenium selenium-webdriver

我是selenium的新手,无法点击名为“Register”的图片,该图片在附加的屏幕截图中突出显示。任何人都可以告诉我为什么在运行时没有识别网页元素以及可以做些什么来识别和点击在它上面?

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


    }
}

Screenshot

1 个答案:

答案 0 :(得分:1)

给定的"注册" img在框架内。所以你首先需要切换框架,然后在该框架内的webelement上执行任何事件。

请在您的代码中添加以下代码行。

     driver1.switchTo().frame("mainFrame"); // switch frame 

        driver1.findElement(By.xpath("//*[@id='lhsNav']/a/img[@src='images/register2.gif']")).click();
        //UserID

// other operation

希望这可以帮助你:)