Python 2.7 Selenium 3.0.2 Python 2.7中的隐式等待与时间睡眠()
我试图在网站上的Python2.7上用Selenium实现一个简单的子加载程序 我在Selenium中实现隐式或显式等待时遇到了严重的问题——这肯定是由于无知而不是代码造成的 我最终选择了销毁所有Python 2.7 Selenium 3.0.2 Python 2.7中的隐式等待与时间睡眠(),python-2.7,selenium,selenium-webdriver,Python 2.7,Selenium,Selenium Webdriver,我试图在网站上的Python2.7上用Selenium实现一个简单的子加载程序 我在Selenium中实现隐式或显式等待时遇到了严重的问题——这肯定是由于无知而不是代码造成的 我最终选择了销毁所有time.sleep()解决方案。该计划的工作,但我想一个更编程的解决方案和一个漂亮的代码 以下是包含隐式和显式等待的代码: user_input = raw_input("Enter the movie of interest: ") profile = SetProfile() browser =
time.sleep()
解决方案。该计划的工作,但我想一个更编程的解决方案和一个漂亮的代码
以下是包含隐式和显式等待的代码:
user_input = raw_input("Enter the movie of interest: ")
profile = SetProfile()
browser = webdriver.Firefox(profile)
# browser.implicitly_wait(30)
WindowSize(400, 400)
browser.get(url)
print "Searching..."
search_bar = browser.find_element_by_id("qSearch")
click_button = browser.find_element_by_class_name("glyphicon")
#wait_1 = WebDriverWait(browser, 50)
#cookie_element = wait_1.until(EC.element_to_be_clickable((By.CLASS_NAME,"cc_btn.cc_btn_accept_all")))
time.sleep(5)
accept_cookies_btn = browser.find_element_by_class_name("cc_btn.cc_btn_accept_all")
search_bar.clear()
accept_cookies_btn.click()
search_bar.send_keys(user_input)
search_bar.send_keys(Keys.RETURN)
time.sleep(10)
#wait_2 = WebDriverWait(browser, 50)
#result_element = wait_2.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "h3.media-heading")))
result = browser.find_element_by_class_name("media-heading")
result.click()
print "Movie found!"
time.sleep(10)
req = requests.get(browser.current_url)
soup = BeautifulSoup(req.text)
link_to_sub = ResultParser(soup)
print "Choosing the best eng. subtitle..."
browser.get(url+link_to_sub[1])
download_button = browser.find_element_by_class_name("btn-icon")
download_button.click()
time.sleep(10)
browser.quit()
print "Enjoy!"
我已经注释掉了我试图使用的等待(显式或隐式)。事实上,如果我使用waits代替time.sleep,Selenium会向我抛出一个ElementNotVisibleException
因此,我是否在等待中做错了什么?
关于隐式等待,Selenium总是在我运行程序时向我抛出ElementNotVisibleException:Message:
(Message为空),不管等待的长度是多少(尝试了5、30和100!)
同样,当我使用显式等待时,Selenium要么抛出ElementNotVisibleException
,要么似乎使用上一页中的DOM。实际上,在最后一个例子中,等待是在点击结果之后,但是我得到了上一页的标题
正如我所说,当我使用time.sleep()
时,Selenium会毫不费力地找到元素
一个简单问题的长消息:为什么我的等待似乎不起作用?您需要为我们提供包含不起作用的等待的代码,以帮助您解释为什么等待不起作用。未注释的等待是否足够?我想去掉它们,这样就只有一段代码,包括可能的等待,我已经包括到目前为止,你是对的。。。我忽略了这些评论,没有看到它们包含了等待。很抱歉