Home > Software engineering >  Elements able to be found using XPATH but not using CSS Selector. Am I searching using the correct v
Elements able to be found using XPATH but not using CSS Selector. Am I searching using the correct v

Time:10-04

I am trying to extract data from multiple pages of search results where the HTML in question looks like so:

<ul>
    <li >...</li>
    <li >...</li>
    <li >...</li>
</ul>

I want to extract the text from the "li" tags, so I have:

text_data = WebDriverWait(driver,10).until(EC.visibility_of_all_element_located((By.XPATH,'Card___StyledLi4-ulg8ho-7.jmevwM')

print(text_data.text)

to wait and target "li" item. However, I get a "TimeoutException" error.

However, if I try to locate a single "li" item using the XPATH under the same conditions, the data is returned which leads me to question if I am inputting the class correctly?

Can anyone tell me what I'm doing wrong? Please let me know if there is any further information, you'd like me to provide.

CodePudding user response:

I believe the XPath for these list items would be //li[@] (or //*[@] if you want all elements with that class rather than just li tags). You can take a look at this cheatsheet and this tutorial for further rules and examples of XPath.

You can also just use CSS Selectors like (By.CSS_SELECTOR, '.Card___StyledLi4-ulg8ho-7.jmevwM') in this case.

CodePudding user response:

You have mentioned the wrong locator type, it should be CSS_SELECTOR and also put a dot '.' in front of element's property, because it is a 'class':

text_data = WebDriverWait(driver,10).until(EC.visibility_of_all_element_located((By.CSS_SELECTOR,'.Card___StyledLi4-ulg8ho-7.jmevwM')
  • Related