Milano
Milano

Reputation: 18745

WebDriver does not recognize element

I'm trying to make Selenium wait for a specific element (near the bottom of the page) since I have to wait until the page is fully loaded.

I'm confused by it's behavior.

I'm not an expert in Selenium but I expect this work:

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

driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)

    def load_page():
        driver.get('http://www.firmy.cz/?geo=0&q=hodinov%C3%BD+man%C5%BEel&thru=sug')
        wait.until(EC.visibility_of_element_located((By.PARTIAL_LINK_TEXT, 'Zobrazujeme')))
        html = driver.page_source
        print html

    load_page()

TIMEOUT:

File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 78, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

I'm just trying to see the HTML of the fully loaded page. It raises TimeoutException but I'm sure that that element is already there. I've already tried another approach.

wait.until(EC.visibility_of_element_located(driver.find_element_by_xpath('//a[@class="companyTitle"]')))

But this approach raises error too:

selenium.common.exceptions.NoSuchElementException:
Message: Unable to locate element:
{"method":"xpath","selector":"//a[@class=\"companyTitle\"]"}

Upvotes: 1

Views: 211

Answers (2)

drets
drets

Reputation: 2805

The main issue in your code is wrong selectors.

If you want to wait till web element with text Zobrazujeme will loaded and then print page source:

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

driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)

def load_page():
    driver.get('http://www.firmy.cz/?geo=0&q=hodinov%C3%BD+man%C5%BEel&thru=sug')
    wait.until(EC.visibility_of_element_located((By.CLASS_NAME , 'switchInfoExt')))
    html = driver.page_source
    print html

load_page()

Upvotes: 0

Dušan Maďar
Dušan Maďar

Reputation: 9909

Loading the site takes a long time, use implicitly waiting.

In this case, when you are interested in the whole HTML, you don't have to wait for a specific element at the bottom of the page.
The load_page function will print the HTML as soon as the whole site is loaded if you give the browser enough time to do this with implicitly_wait().

from selenium import webdriver


driver = webdriver.Firefox()
# wait max 30 seconds till any element is located
# or the site is loaded
driver.implicitly_wait(30)


def load_page():
    driver.get('http://www.firmy.cz/?geo=0&q=hodinov%C3%BD+man%C5%BEel&thru=sug')
    html = driver.page_source
    print html

load_page()

Upvotes: 0

Related Questions