Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use mutiple attributes (including a partial string match) with find_elements in Selenium for Python

Currently I have this line of code which correctly selects this type of object on the webpage I’m trying to manipulate with Selenium:

pointsObj = driver.find_elements(By.CLASS_NAME,'treeImg')

What I need to do is add in a partial string match condition as well which looks in the section "CLGV (AHU-01_ahu_ChilledWtrVlvOutVolts)" in the line below.

<span class="treeImg v65point" style="cursor:pointer;">CLGV (AHU-01_ahu_ChilledWtrVlvOutVolts)</span>

I found online there’s the ChainedBy option but I can’t think of how to reference that text in the span. Do I need to use XPath? I tried that for a second but I couldn’t think of how to parse it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Refering both the CLASS_NAME and the innerText you can use either of the following locator strategies:

  • xpath using the classname treeImg and partial innerText:

    pointsObj = driver.find_elements(By.XPATH,"//span[contains(@class, 'treeImg') and contains(., 'AHU-01_ahu_ChilledWtrVlvOutVolts')]")
    
  • xpath using all the classnames and entire innerText:

    pointsObj = driver.find_elements(By.XPATH,"//span[@class='treeImg v65point' and text()='CLGV (AHU-01_ahu_ChilledWtrVlvOutVolts)']")
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading