如何使用selenium WebDriver单击图像

问题描述 投票:0回答:1

我是selenium的新手,无法点击名为“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


    }
}

Screenshot

java selenium selenium-webdriver
1个回答
1
投票

给定的“Register”img是内部框架。所以你需要首先切换帧然后执行在该帧内的webelement上的任何事件。

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

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

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

// other operation

希望这对你有所帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.