selenium First project in selenium with Java Getting Elements in Selenium


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="">google</a>
<p class="p1">
This is a paragraph

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

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

Now, we can click on this, by;

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

We can also get Elements by tags, like

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