Home >Backend Development >Python Tutorial >How to Use Selenium's EC.presence_of_element_located() with Multiple Class Names?
When web scraping dynamic HTML pages, using WebDriverWait with EC.presence_of_element_located() allows us to wait for a particular element to appear before retrieving data. Typically, we specify elements by their IDs. However, in certain scenarios, we may need to specify elements by their classes instead.
The original code attempts to locate an element by its class using EC.presence_of_element_located((By.class, "ng-binding ng-scope")):
element = WebDriverWait(driver,100).until(EC.presence_of_element_located((By.class, "ng-binding ng-scope")))
However, this syntax will not work as it does not adhere to WebDriver's Locator Syntax rules. The By.class method doesn't support multiple class names as an argument.
To resolve this issue, we can use the presence_of_element_located() function along with By.CSS_SELECTOR or By.XPATH to specify both the ID and class attributes as follows:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".ng-binding.ng-scope#tabla_evolucion")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//*[@class='ng-binding ng-scope' and @id='tabla_evolucion']")))
By combining the ID and class attributes, we can precisely locate the desired element even when only the class is specified.
The above is the detailed content of How to Use Selenium's EC.presence_of_element_located() with Multiple Class Names?. For more information, please follow other related articles on the PHP Chinese website!