如何单击分页中的箭头元素,这会引发通过 WebDriverWait 不可见的元素
How to click on the arrow element in pagination which raises Element not visible through WebDriverWait
我尝试点击此网页的下一页link:https://www.kumon.co.uk/find-a-tutor/
在您键入伦敦、曼彻斯特或其他任何地方之前,此页面将为空 :)
源代码如下所示:
<nav class="visible-xs-block">
<ul class="pager">
<li>
<a data-page="2" href="https://www.kumon.co.uk/find-a-tutor/?centre_search=london&page=2"><small><i class="fa fa-chevron-right"></i></small></a>
</li>
</ul>
</nav>
我尝试通过两种方式点击进入下一页:
初审:
browser.find_elements_by_xpath("//*[@class='fa fa-chevron-right']/../..")[2].click()
错误:
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
二审:
WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='fa fa-chevron-right']/../.."[2]))).click()
错误:
selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
重要的是我找不到关于第二条错误消息的任何文档,这就是为什么我 post 我在 Whosebug 上的第一条消息 :)
仅供参考,当我删除 click() 时,元素已很好地本地化,所以这是看起来很糟糕的 click() :/
我找到了这个处理 Javascript 的解决方案,这对我来说可能很酷,但我真的不知道如何实现它 =>
你怎么看?
你有什么想法可以帮助我吗?不幸的是,我在这个问题上停留了 2 天:(
编辑:
这段代码:
while browser.find_element_by_xpath("//ul[@class='pagination']//li[last()]/a/small"):
action = ActionChains(browser)
search_button = WebDriverWait(browser, 20).until(
EC.element_to_be_clickable((By.XPATH, "//ul[@class='pagination']//li[last()]/a/small")))
action.move_to_element(search_button)
sleep(1)
search_button.click()
引发错误selenium.common.exceptions.StaleElementReferenceException:消息:过时的元素引用:元素未附加到页面文档
而且我不确定使用 try...except 是解决它的最佳解决方案,对吗?
提前致谢,
尼古拉斯.
要在 >
图标上调用 click()
以移动到下一页,您需要引入 WebDriverWait 使所需的元素可点击,您可以使用以下解决方案:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='pagination']//li[last()]/a/small"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
看起来使用的xpath不正确。最好在浏览器控制台中交叉检查 xpath。上面回答 @DebanjanB 中提供的 xpath 很好。尝试一次:在 chrome->F12->Console 中:粘贴下面一个并回车。
$x("//ul[@class='pagination']//li[last()]/a/small")
它必须获取所需的元素。类似地尝试您在发布的问题中使用的所有 xpath,它们获取错误的元素导致执行异常。
我尝试点击此网页的下一页link:https://www.kumon.co.uk/find-a-tutor/ 在您键入伦敦、曼彻斯特或其他任何地方之前,此页面将为空 :)
源代码如下所示:
<nav class="visible-xs-block">
<ul class="pager">
<li>
<a data-page="2" href="https://www.kumon.co.uk/find-a-tutor/?centre_search=london&page=2"><small><i class="fa fa-chevron-right"></i></small></a>
</li>
</ul>
</nav>
我尝试通过两种方式点击进入下一页:
初审:
browser.find_elements_by_xpath("//*[@class='fa fa-chevron-right']/../..")[2].click()
错误:
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
二审:
WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, "//*[@class='fa fa-chevron-right']/../.."[2]))).click()
错误:
selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
重要的是我找不到关于第二条错误消息的任何文档,这就是为什么我 post 我在 Whosebug 上的第一条消息 :)
仅供参考,当我删除 click() 时,元素已很好地本地化,所以这是看起来很糟糕的 click() :/
我找到了这个处理 Javascript 的解决方案,这对我来说可能很酷,但我真的不知道如何实现它 =>
你怎么看?
你有什么想法可以帮助我吗?不幸的是,我在这个问题上停留了 2 天:(
编辑:
这段代码:
while browser.find_element_by_xpath("//ul[@class='pagination']//li[last()]/a/small"):
action = ActionChains(browser)
search_button = WebDriverWait(browser, 20).until(
EC.element_to_be_clickable((By.XPATH, "//ul[@class='pagination']//li[last()]/a/small")))
action.move_to_element(search_button)
sleep(1)
search_button.click()
引发错误selenium.common.exceptions.StaleElementReferenceException:消息:过时的元素引用:元素未附加到页面文档
而且我不确定使用 try...except 是解决它的最佳解决方案,对吗?
提前致谢, 尼古拉斯.
要在 >
图标上调用 click()
以移动到下一页,您需要引入 WebDriverWait 使所需的元素可点击,您可以使用以下解决方案:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='pagination']//li[last()]/a/small"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
看起来使用的xpath不正确。最好在浏览器控制台中交叉检查 xpath。上面回答 @DebanjanB 中提供的 xpath 很好。尝试一次:在 chrome->F12->Console 中:粘贴下面一个并回车。
$x("//ul[@class='pagination']//li[last()]/a/small")
它必须获取所需的元素。类似地尝试您在发布的问题中使用的所有 xpath,它们获取错误的元素导致执行异常。