python selenium : selenium.common.exceptions.TimeoutException: Message: line 20 -
from selenium import webdriver selenium.webdriver.support.ui import webdriverwait import time driver = webdriver.firefox() driver.get("http://www.youtube.com") assert "youtube" in driver.title def waiter(browser): elements = browser.find_element_by_xpath(filterbutton) if len(elements) != 0: return elements return false search = "//input[@id='masthead-search-term']" searchbutton = "//button[@id='search-btn']" filterbutton = "//button[@class='yt-uix-button yt-uix-button-size-small yt-uix-button-default filter-button yt-uix-expander-head yt-uix-button-toggled']" textfieldelement = webdriverwait(driver, 10).until(lambda driver1: driver.find_element_by_xpath(search)) textfieldelement.clear() textfieldelement.send_keys("how iron clothes") searchbuttonelement = webdriverwait(driver, 10).until(lambda driver1: driver.find_element_by_xpath(searchbutton)) searchbuttonelement.click() filterbuttonelement = webdriverwait(driver, 20).until(waiter) filterbuttonelement.clickandwait() time.sleep(10) driver.quit()
i trying crawl through youtube site using selenium python binding gives error on line 20 of timeout exception error . think due span tag not visible. please provide me method how resolve problem
error generated : // selenium.common.exceptions.timeoutexception: message: line 20
in general, xpath expression fragile - don't rely on layout-oriented classes yt-uix-button-size-small
or yt-uix-expander-head
. instead, instance, rely on button text "filters".
instead of using custom wait functions, use built-in expected conditions:
# -*- coding: utf-8 -*- selenium import webdriver selenium.webdriver.common.by import selenium.webdriver.support import expected_conditions ec selenium.webdriver.support.ui import webdriverwait driver = webdriver.firefox() driver.get("http://www.youtube.com") wait = webdriverwait(driver, 10) textfieldelement = wait.until(ec.visibility_of_element_located((by.id, "masthead-search-term"))) textfieldelement.clear() textfieldelement.send_keys("how iron clothes") searchbuttonelement = wait.until(ec.visibility_of_element_located((by.id, "search-btn"))) searchbuttonelement.click() filterbuttonelement = wait.until(ec.visibility_of_element_located((by.xpath, "//button[span = 'filters']"))) filterbuttonelement.click()
Comments
Post a Comment