selenium First project in selenium with Java Getting Elements in Selenium

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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"));


Got any selenium Question?