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

Time:04-08

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  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.

CodePudding user response:

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)']")
    
  • Related