Home > Blockchain >  Python - Unable to locate an element by class_name but able to locate by XPATH
Python - Unable to locate an element by class_name but able to locate by XPATH

Time:01-09

I'm trying to locate an element by class_name but i'm getting an error "no such element"
When i'm trying to locate the same element by XPATH it works

Why it fails to locate the element by his class_name?

this are the 2 locators that i tried:
1: txt_company_name = (By.CLASS_NAME, 'crm-entity-widget-content-input crm-entity-widget-content-search-input')

2: txt_company_name = (By.XPATH, '//*[@]')

This is the HTML:

<input type="text" placeholder="Company name, phone or email"  autocomplete="nope">

CodePudding user response:

The reason is CLASS_NAME with spaces do not work in Selenium.

Basically, if you see a class name like this in HTML:

crm-entity-widget-content-input crm-entity-widget-content-search-input

what it means is, that it is combination of this class crm-entity-widget-content-input and crm-entity-widget-content-search-input

so you will either have to use the below CSS_SELECTOR

input.crm-entity-widget-content-input

or

input.crm-entity-widget-content-search-input

or even the below should work, provided it is unique in HTMLDOM:

.crm-entity-widget-content-input.crm-entity-widget-content-search-input

basically, we use . to represent a class in CSS, so you can remove the space and put a . and also in the starting and that should help you find the right CSS.

or the XPath that you've should suffice. However, CSS has more precedence than XPath.

  •  Tags:  
  • Related