How can I filter elements that have the same class?
<html>
<body>
<p class="content">Link1.</p>
</body>
</html>
<html>
<body>
<p class="content">Link2.</p>
</body>
</html>
How can I filter elements that have the same class?
<html>
<body>
<p class="content">Link1.</p>
</body>
</html>
<html>
<body>
<p class="content">Link2.</p>
</body>
</html>
You can try to get the list of all elements with class = "content"
by using find_elements_by_class_name:
a = driver.find_elements_by_class_name("content")
Then you can click on the link that you are looking for.
a
as an empty list if you immediately reassign it? –
Coadjutrix find_element_by_class_name
–
Keyes .click()
attribute doesn't work! –
Helga find_element_by_*
and find_elements_by_*
are removed in Selenium 4.3.0. Use find_element
instead. Look at all change logs here. –
Miksen find_element()
throws an exception if the element doesn't exist. –
Brandtr find_elements(By.CLASS_NAME, 'foo')
to get a list of elements –
Gauleiter By.CLASS_NAME was not yet mentioned:
from selenium.webdriver.common.by import By
driver.find_element(By.CLASS_NAME, "content")
This is the list of attributes which can be used as locators in By:
CLASS_NAME
CSS_SELECTOR
ID
LINK_TEXT
NAME
PARTIAL_LINK_TEXT
TAG_NAME
XPATH
As per the HTML:
<html>
<body>
<p class="content">Link1.</p>
</body>
<html>
<html>
<body>
<p class="content">Link2.</p>
</body>
<html>
Two(2) <p>
elements are having the same class content
.
So to filter the elements having the same class i.e. content
and create a list you can use either of the following Locator Strategies:
Using class_name
:
elements = driver.find_elements_by_class_name("content")
Using css_selector
:
elements = driver.find_elements_by_css_selector(".content")
Using xpath
:
elements = driver.find_elements_by_xpath("//*[@class='content']")
Ideally, to click on the element you need to induce WebDriverWait for the visibility_of_all_elements_located()
and you can use either of the following Locator Strategies:
Using CLASS_NAME
:
elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "content")))
Using CSS_SELECTOR
:
elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".content")))
Using XPATH
:
elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@class='content']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can find a couple of relevant discussions in:
find_element_by_*
and find_elements_by_*
are removed in Selenium 4.3.0. Use find_element
instead." –
Brandtr Use nth-child, for example: http://www.w3schools.com/cssref/sel_nth-child.asp
driver.find_element(By.CSS_SELECTOR, 'p.content:nth-child(1)')
or http://www.w3schools.com/cssref/sel_firstchild.asp
driver.find_element(By.CSS_SELECTOR, 'p.content:first-child')
The most simple way is to use find_element_by_class_name('class_name')
The first answer has been deprecated, and the other answers only return one result. This is the correct answer:
driver.find_elements(By.CLASS_NAME, "content")
The question was related to "elements" plural, most are for element. To wait for the element and then iterate over each:
selectableEls = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "content")))
for el in selectableEls:
print(el)
In Python, Selenium provides a method called find_elements_by_class_name to find elements with the same class name. This method returns a list of all elements that match the provided class name.
Let's say you're trying to find all paragraphs (
) with the class "content". Here's how you can do it:
from selenium import webdriver
driver = webdriver.Firefox() # replace with the browser driver of your choice
driver.get('https://www.softwaretestingsapiens.com/finding-elements-by-class-name-with-selenium-in-python/')
elements = driver.find_elements_by_class_name('content')
for el in elements: print(el.text)
This script will print the text inside each
tag with the class "content". In your case, it should print "Link1." and "Link2.".
Remember, Selenium requires a specific driver to interface with the chosen browser. Firefox requires geckodriver, which needs to be installed before the above script can be run.
Also, keep in mind that find_elements_by_class_name returns a list, even if there's only one match. If you're certain there's only one element of interest, use find_element_by_class_name (note the lack of 's' in 'element') to get the WebElement directly.
Lastly, it's recommended to add some error handling to your script to manage situations where the elements are not found. This will make your code more robust and easier to debug.
Happy coding!
© 2022 - 2024 — McMap. All rights reserved.