Python WebDriverWait未按预期工作
我正在与selenium合作收集一些数据 在我点击的页面上有一个按钮,上面写着“定制”。这个按钮为我打开一个窗口,在那里我可以选择我的列 这个新窗口有时需要一些时间才能打开(大约5秒)。所以我用了Python WebDriverWait未按预期工作,python,selenium,web-scraping,webdriverwait,expected-condition,Python,Selenium,Web Scraping,Webdriverwait,Expected Condition,我正在与selenium合作收集一些数据 在我点击的页面上有一个按钮,上面写着“定制”。这个按钮为我打开一个窗口,在那里我可以选择我的列 这个新窗口有时需要一些时间才能打开(大约5秒)。所以我用了 WebDriverWait 延迟20秒。但有时它无法选择“在新窗口上查找元素”,即使该元素可见。这种情况十次只发生一次,其余时间正常工作 我在其他地方也使用了相同的功能(WebDriverWait),它的工作原理与预期一致。我的意思是,它等待元素变得可见,然后在找到它的那一刻单击它 我的问题是为什
WebDriverWait
延迟20秒。但有时它无法选择“在新窗口上查找元素”,即使该元素可见。这种情况十次只发生一次,其余时间正常工作
我在其他地方也使用了相同的功能(WebDriverWait),它的工作原理与预期一致。我的意思是,它等待元素变得可见,然后在找到它的那一刻单击它
我的问题是为什么新窗口上的元素不可见,即使我在等待元素可见。在此补充,我已经尝试增加延迟时间,但我仍然偶尔会遇到这种错误
我的密码在这里
def wait_for_elem_xpath(self, delay = None, xpath = ""):
if delay is None:
delay = self.delay
try:
myElem = WebDriverWait(self.browser, delay).until(EC.presence_of_element_located((By.XPATH , xpath)))
except TimeoutException:
print ("xpath: Loading took too much time!")
return myElem
select_all_performance = '//*[@id="mks"]/body/div[7]/div[2]/div/div/div/div/div[2]/div/div[2]/div[2]/div/div[1]/div[1]/section/header/div'
self.wait_for_elem_xpath(xpath = select_all_performance).click()
当您等待元素并在尝试调用
click()
方法时向前移动,而不是使用presence\u of\u element\u located()
方法时,您需要使用以下方法:
try:
myElem = WebDriverWait(self.browser, delay).until(EC.element_to_be_clickable((By.XPATH , xpath)))
class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)
Parameter : locator - used to find the element returns the WebElement once it is located
Description : An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible or interactable (i.e. clickable).
class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)
Parameter : locator - used to find the element returns the WebElement once it is located and visible
Description : An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)
Parameter : locator - used to find the element returns the WebElement once it is visible, enabled and interactable (i.e. clickable).
Description : An Expectation for checking an element is visible, enabled and interactable such that you can click it.
更新 根据您在评论中的反问,以下是三种方法的详细信息: 元素的存在位置 定义如下:
try:
myElem = WebDriverWait(self.browser, delay).until(EC.element_to_be_clickable((By.XPATH , xpath)))
class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)
Parameter : locator - used to find the element returns the WebElement once it is located
Description : An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible or interactable (i.e. clickable).
class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)
Parameter : locator - used to find the element returns the WebElement once it is located and visible
Description : An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)
Parameter : locator - used to find the element returns the WebElement once it is visible, enabled and interactable (i.e. clickable).
Description : An Expectation for checking an element is visible, enabled and interactable such that you can click it.
位于的元素的可见性
定义如下:
try:
myElem = WebDriverWait(self.browser, delay).until(EC.element_to_be_clickable((By.XPATH , xpath)))
class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)
Parameter : locator - used to find the element returns the WebElement once it is located
Description : An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible or interactable (i.e. clickable).
class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)
Parameter : locator - used to find the element returns the WebElement once it is located and visible
Description : An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)
Parameter : locator - used to find the element returns the WebElement once it is visible, enabled and interactable (i.e. clickable).
Description : An Expectation for checking an element is visible, enabled and interactable such that you can click it.
元素可点击
定义如下:
try:
myElem = WebDriverWait(self.browser, delay).until(EC.element_to_be_clickable((By.XPATH , xpath)))
class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)
Parameter : locator - used to find the element returns the WebElement once it is located
Description : An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible or interactable (i.e. clickable).
class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)
Parameter : locator - used to find the element returns the WebElement once it is located and visible
Description : An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)
Parameter : locator - used to find the element returns the WebElement once it is visible, enabled and interactable (i.e. clickable).
Description : An Expectation for checking an element is visible, enabled and interactable such that you can click it.
你能解释一下为什么它能工作,而其他功能却不能。