[英]How to go on last page from number of pages using selenium python?
This is the Screenshot of my UI and HTML DOM.这是我的 UI 和 HTML DOM 的屏幕截图。
I want to go to the last page one this I can do is pressing next button many times, but I don't know how many time it should be pressed, so i don't think so its a better way.我想翻到最后一页,我可以做的就是多次按下下一步按钮,但我不知道应该按下多少次,所以我认为这不是更好的方法。 Can you suggest me better way using selenium commands.
你能建议我使用 selenium 命令更好的方法吗?
I used this command (pressing next continuously):我使用了这个命令(连续按下一步):
driver.find_element_by_xpath('html/body/div[2]/div/section[8]/div/div/div[3]/div/div[2]/table/tfoot/tr/td/div/ul/li[5]/a').click()
You can use try/except
like:您可以使用
try/except
例如:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
while True:
try:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//li[not(@class="disabled")]/a[contains(text(), "Next")]'))).click()
except TimeoutException:
break
This should allow you to click Next
button until last page (until button become disabled)这应该允许您单击“
Next
按钮直到最后一页(直到按钮被禁用)
PS You should not use absolute XPath
, eg html/body/div[2] ... /div/ul/li[5]/a
as it's sensitive to changes in DOM
. PS 你不应该使用绝对
XPath
,例如html/body/div[2] ... /div/ul/li[5]/a
因为它对DOM
变化很敏感。 Use target element's attributes or its parent/child nodes to create relative XPath
使用目标元素的属性或其父/子节点来创建相对
XPath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.