Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python selenium:selenium.common.exceptions.TimeoutException:消息:第20行_Python_Python 3.x_Selenium_Selenium Webdriver - Fatal编程技术网

python selenium:selenium.common.exceptions.TimeoutException:消息:第20行

python selenium:selenium.common.exceptions.TimeoutException:消息:第20行,python,python-3.x,selenium,selenium-webdriver,Python,Python 3.x,Selenium,Selenium Webdriver,我试图使用selenium python绑定在youtube网站上爬行,但它在超时异常错误的第20行给出了一个错误。我认为这是由于span标记不可见。所以请给我一个解决这个问题的方法 生成错误://selenium.common.exceptions.TimeoutException: 信息:第20行 一般来说,XPath表达式非常脆弱-不要依赖面向布局的类,比如yt uix按钮大小小或yt uix扩展器头。相反,例如,依赖按钮文本,即“过滤器” 不要使用自定义等待函数,请使用: 或者更好的是,

我试图使用selenium python绑定在youtube网站上爬行,但它在超时异常错误的第20行给出了一个错误。我认为这是由于span标记不可见。所以请给我一个解决这个问题的方法

生成错误://selenium.common.exceptions.TimeoutException: 信息:第20行


一般来说,XPath表达式非常脆弱-不要依赖面向布局的类,比如
yt uix按钮大小小
yt uix扩展器头
。相反,例如,依赖按钮文本,即“过滤器”

不要使用自定义等待函数,请使用:


或者更好的是,只依赖特定的标识符,专门为selenium制作(如果您可以控制项目的HTML标记),因为任何前端内容的文本都可能发生变化。
from selenium import webdriver
from 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 to iron the 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()
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from 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 to iron the 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()