close

How do you use EC.presence_of_element_located((By.ID, “myDynamicElement”)) except to specify class not ID

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do you use EC.presence_of_element_located((By.ID, “myDynamicElement”)) except to specify class not ID in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How do you use EC.presence_of_element_located((By.ID, “myDynamicElement”)) except to specify class not ID?

  1. How do you use EC.presence_of_element_located((By.ID, “myDynamicElement”)) except to specify class not ID

    You can find a detailed discussion in Invalid selector: Compound class names not permitted using find_element_by_class_name with Webdriver and Python

  2. use EC.presence_of_element_located((By.ID, “myDynamicElement”)) except to specify class not ID

    You can find a detailed discussion in Invalid selector: Compound class names not permitted using find_element_by_class_name with Webdriver and Python

Method 1

The relevant HTML would have helped us to construct a more canonical answer. However to start with your first line of code:

element = WebDriverWait(driver,100).until(EC.presence_of_element_located(
  (By.ID, "tabla_evolucion")))

is pretty much legitimate where as the second line of code:

element = WebDriverWait(driver,100).until(EC.presence_of_element_located(
  (By.class, "ng-binding ng-scope")))

Will raise an error as:

Message: invalid selector: Compound class names not permitted

as you can’t pass multiple classes through By.class.

You can find a detailed discussion in Invalid selector: Compound class names not permitted using find_element_by_class_name with Webdriver and Python


Solution

You need to take care of a couple of things as follows:

  • Without any visibility to your usecase, functionally inducing WebDriverWait in association with EC as presence_of_element_located() merely confirms the presence of the element within the DOM Tree. Presumably moving ahead either you need to get the attributes e.g. valueinnerText, etc or you would interact with the element. So instead of presence_of_element_located() you need to use either visibility_of_element_located() or element_to_be_clickable()

You can find a detailed discussion in WebDriverWait not working as expected

  • For an optimum result you can club up the ID and CLASS attributes and you can use either of the following Locator Strategies:
  • Using CSS_SELECTOR:
  element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located(
    (By.CSS_SELECTOR, ".ng-binding.ng-scope#tabla_evolucion")))
  • Using XPATH:
  element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located(
    (By.XPATH, "//*[@class='ng-binding ng-scope' and @id='tabla_evolucion']")))

Method 2

It’s in the docs.

Set of supported locator strategies.
CLASS_NAME = ‘class name’
CSS_SELECTOR = ‘css selector’
ID = ‘id’
LINK_TEXT = ‘link text’
NAME = ‘name’
PARTIAL_LINK_TEXT = ‘partial link text’
TAG_NAME = ‘tag name’
XPATH = ‘xpath’

Note: What you have in your code is not a class, it’s two classes. That won’t work if you use By.CLASS_NAME() because it expects only a single class. What you want instead is a CSS selector

EC.presence_of_element_located((By.CSS_SELECTOR, ".ng-binding.ng-scope")))

In CSS selector syntax, a . indicates a class. See the W3C docs for more info on the CSS selector syntax.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read