Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium code to wait until CSS class is available and extract text in Python

I have looked at some questions on here that are somewhat related but none of them are concerned with finding a class and then extracting text that is part of that class.

My goal is to automate the process of entry into a website that finds emails, given name and domain names. So far I have the code to automate the entry of the name and domain, and then click on "search", but my issue is waiting for a result to load. I basically want my code wot wait until the CSS class "one" is present and then extract the text related to this class, e.g. for the following snippet:

<h3 class="one">Success</h3>

Extract the text "Success".

like image 565
ohbrobig Avatar asked Mar 21 '15 02:03

ohbrobig


People also ask

What is fluent wait in Selenium with Python?

Fluent Wait in Selenium marks the maximum amount of time for Selenium WebDriver to wait for a certain condition (web element) becomes visible. It also defines how frequently WebDriver will check if the condition appears before throwing the “ElementNotVisibleException”.

Does Selenium wait for page to load?

There are three ways to implement Selenium wait for page to load: Using Implicit Wait. Using Explicit Wait. Using Fluent Wait.

What is implicit wait in selenium Python?

Implicit Waits. An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available. The default setting is 0 (zero). Once set, the implicit wait is set for the life of the WebDriver object.


1 Answers

You need to explicitly wait for the element to become visible:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
h3 = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h3.one")))
print h3.text
like image 120
alecxe Avatar answered Sep 28 '22 17:09

alecxe