【发布时间】:2019-04-27 07:24:37
【问题描述】:
我希望实现 Selenium WebDriverWaits 的方法链。
首先,这个实现单个 WebDriverWait 的代码块非常完美:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.facebook.com')
element = WebDriverWait(driver, 5).until(lambda x: x.find_element_by_xpath("//input[@id='email']"))
element.send_keys("method_chaining")
根据我目前的要求,我必须实现两个 WebDriverWait 实例的链接,因为我的想法是获取从第一个 WebDriverWait 返回的元素作为(链接的)第二个 WebDriverWait.
为了实现这一点,我按照method chaining in python 的讨论尝试通过方法链 使用Pipe - Python library to use infix notation in Python 来使用Python 的lambda 函数。
这是我的代码试用版:
from pipe import *
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://www.facebook.com')
element = WebDriverWait(driver,15).until((lambda driver: driver.find_element_by_xpath("//input[@id='email']"))
| where(lambda driver: driver.find_element_by_css_selector("table[role='presentation']")))
element.send_keys("method_chaining")
我看到一个错误:
DevTools listening on ws://127.0.0.1:52456/devtools/browser/e09c1d5e-35e3-4c00-80eb-cb642fa273ad
Traceback (most recent call last):
File "C:\Users\Soma Bhattacharjee\Desktop\Debanjan\PyPrograms\python_pipe_example.py", line 24, in <module>
| where(lambda driver: driver.find_elements(By.CSS_SELECTOR,"table[role='presentation']")))
File "C:\Python\lib\site-packages\pipe.py", line 58, in __ror__
return self.function(other)
File "C:\Python\lib\site-packages\pipe.py", line 61, in <lambda>
return Pipe(lambda x: self.function(x, *args, **kwargs))
File "C:\Python\lib\site-packages\pipe.py", line 271, in where
return (x for x in iterable if (predicate(x)))
TypeError: 'function' object is not iterable
关注了以下讨论:
- python3 TypeError: 'function' object is not iterable
- TypeError: 'function' object is not iterable' Python 3
仍然不知道我错过了什么。
谁能指导我哪里出错了?
【问题讨论】:
-
为两个预期条件实现 WebDriverWait... 哪些预期条件?
-
@Andersson 好问题...(在我的回答中,我假设
presence_of_element_located和element_to_be_clickable)。 -
@DebanjanB 在我的回答中,你可以看到你的一些问题,我相信你会有更优雅的方式来实现它......
-
@Andersson 感谢您的关注。为简洁起见,我已经更新了问题。如果您需要更多详细信息,请告诉我。
-
问题不断变化,目标不断变化:)。最新的编辑是“因为想法是获取从第一个 WebDriverWait 返回的元素作为(链接的)第二个 WebDriverWait 的输入。” - 但在您的尝试示例中,您没有使用第一个元素,等待基于
driver- 所以它来自 DOM 的顶部。
标签: python selenium lambda method-chaining webdriverwait