1

I tried

driver.find_elements_by_xpath("//*[contains(text()),'panel')]") 

but it's only pulling through a singular result when there should be about 25.

I want to find all the xpath id's containing the word panel on a webpage in selenium and create a list of them. How can I do this?

1
  • Can you show us the example of html with the objects you need to look up? Commented May 19, 2020 at 9:24

2 Answers 2

1

The xpath in your question has an error, not sure if it is a typo but this will not fetch any results. There is an extra parantheses.

Instead of :

driver.find_elements_by_xpath("//*[contains(text()),'panel')]") 

It should be :

driver.find_elements_by_xpath("//*[contains(text(),'panel')]") 

Tip: To test your locators(xpath/CSS) - instead of directly using it in code, try it out on a browser first. Example for Chrome:

  1. Right click on the web page you are trying to automate
  2. Click Inspect and do a CTRL-F.
  3. Type in your xpath and press ENTER

You should be able to scroll through all the matched elements and also verify the total match count

Sign up to request clarification or add additional context in comments.

Comments

0

To collect all the elements containing a certain text e.g. panel using Selenium you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use the following Locator Strategy:

  • Using XPATH and contains():

    elements = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[contains(., 'panel')]")))
    
  • 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
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.