如何在Selenium中选择图像链接

时间:2017-03-18 09:35:20

标签: c# selenium selenium-chromedriver

这是检查时显示的代码:

<div class="modulecontent-btn"> ==$0    
<a href="addmodule.php?course=81318&amp;instance=51828" class="btn btn-edit btn-flat" data-toggle="tooltip" title="" data-original-title="Edit Module">
        <img src="images/icons/pencil_100px.png" width="22" height="22"> 
    </a>
</div>

这是我尝试但失败的代码:

driver.FindElement(By.XPath("//img[@src = 'images/icons/pencil_100px.png']")).Click();
driver.FindElement(By.XPath("//a[@class='btn btn-edit btn-flat']")).Click(); 
driver.FindElement(By.XPath("//button[contains(text(), 'Edit Module')]")).Click();

我错误地输入了任何值吗?此外,我意识到href链接不是一致的链接,因此我无法不断选择它。我想到了点击图片却失败了

1 个答案:

答案 0 :(得分:0)

好像你的img xpath有问题;尝试:

public class LoginModel {
    Connection connection;
    public LoginModel() {
        connection = SqliteConnection.Connector();
        if(connection == null) {
            System.out.println("Connection to DB Failed");
            System.exit(1);          
        }
    }

    public boolean isDbConnected() {
        try {
            return !connection.isClosed();
        } catch (SQLException ex) {
            Logger.getLogger(LoginModel.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean isLogin(String type, String user, String pass) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String query = "select * from users where type = ? and username = ? and password = ?";
        try {
            preparedStatement = connection.prepareStatement(query);
            preparedStatement.setString(1, type);
            preparedStatement.setString(2, user);
            preparedStatement.setString(3, pass);       
            resultSet = preparedStatement.executeQuery();

            //if returns result from db
            if(resultSet.next()) {
                return true;
            }
            else {
                return false;
            }
        }
        catch(Exception e) {
            System.out.println(e);
            return false;            
        }
        finally {
            preparedStatement.close();
            resultSet.close();
        }
    }
}