selenium First project in selenium with Java Getting Elements in Selenium


Example

Every Html-Element in Selenium is called a WebElement. For example, a p tag would be a WebElement, an a tag would be a WebElement, etc. Consider the following html Structure:

<a id="link1" href="https://www.google.com">google</a>
<p class="p1">
This is a paragraph
</p>

Now, if we wanted to get the a tag, we could do

WebElement link = driver.findElement(By.id("link1"));

Now, we can click on this, by

link.click();

Lets take another example. If we wanted the text of the p tag, ie, "This is a paragraph", we can do

WebElement p = driver.findElement(By.className("p1"));
System.out.println(p.getText());

We can also get Elements by tags, like

WebElement tag = driver.findElement(By.tagName("a"));