简体   繁体   中英

how to get text from image logo on Login page using Selenium Webdriver with java

HTML Code below:

<a href="http://mysite.com.au" target="_blank"><img src="/Images/mysite-logo.png" alt="my site"></a>
<img src="/Images/my-site.png" alt="My Site Text">

I tried following to get the text from 'alt' attribute in image:

1st Approach:->

 @FindBy(xpath="//img[@alt='My Site Text']")
 WebElement loginpage_title;
 System.out.println("Title of Login Page is" + loginpage_title.getText());

2nd Approach:->

@FindBy(tagName="img")
 WebElement loginpage_logo;
 String loginpage_title=loginpage_logo.getAttribute("alt");
 System.out.println("Title of Login Page is" + loginpage_title);

None of the above mentioned approach is working for me.

Please provide the solution. Thanks.

As discussed, it should be one of these approach:

1st Approach:->

In first approach, the change is to use getAttribute function to get value of alt attribute .

 @FindBy(xpath="//img[@alt='My Site Text']")
 WebElement loginpage_title;
 System.out.println("Title of Login Page is" + 
       loginpage_title.getAttribute("alt") );

2nd Approach:->

In second approach, the change is to use @FindAllBy instead of @FindBy

@FindAllBy(tagName="img")
 List<WebElement> webelements;
 WebElement loginpage_logo = webelements.get(1); //need to be chaned according to the html
 String loginpage_title=loginpage_logo.getAttribute("alt");
 System.out.println("Title of Login Page is" + loginpage_title);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM