- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I get the src of an image in Selenium?
We can get the source of an image in Selenium. An image in an html document has <img> tagname. Each image also has an attribute src which contains the source of image in the page.
To fetch any attribute in Selenium, we have to use the getAttribute() method. The method takes the attribute name as a parameter. So to get the src attribute, we have to write getAttribute("src").
Example
Code Implementation.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class Imagesrc{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // identify image WebElement l =driver.findElement(By.xpath("//img[@title='Tutorialspoint']")); //getAttribute() to get src of image System.out.println("Src attribute is: "+ l.getAttribute("src")); driver.quit(); } }
Output
- Related Articles
- How do I get the resource id of an image if I know its name in Android?
- How do I set the Selenium webdriver get timeout?
- How do I get the resource id of an image if I know its name in Android using Kotlin?
- How do I get current URL in Selenium Webdriver 2 Python?
- How do I get the index of an item in Tkinter.Listbox?
- How to get the value of src attribute in jQuery?
- How can I set the content of an iframe without src using jQuery?
- How do I resolve the ElementNotInteractableException in Selenium WebDriver?
- How do I use Selenium IDE?
- How do I download Selenium RC?
- How do I get a parent HTML Tag with Selenium WebDriver using Java?
- How do I open Chrome in selenium WebDriver?
- How do I switch to the active tab in Selenium?
- How do I find an element that contains specific text in Selenium Webdriver?
- How do I verify that an element does not exist in Selenium 2?

Advertisements