硒不执行点击操作

时间:2019-07-11 11:00:12

标签: python selenium xpath css-selectors webdriverwait

我正在尝试抓取多页表格。通过单击“下一页按钮”获得下一页(请参见代码段)。

<a class="botons" id="btn2" href="javascript:void(0)">
 Next Page  
 <i class="fas fa-long-arrow-alt-right"></i>
</a>

Selenium通过以下代码找到“按钮”并且“单击”没有问题:

btn_next = self.browser.find_element_by_partial_link_text("Next Page")
btn_next.click()

但是,页面只会刷新,并且表格不会更新到其下一页。

这里有什么问题的线索吗?

编辑:可以在https://www.proxy-list.download/HTTPS上找到表格

Edit2:

chrome_options = Options()
chrome_options.add_argument("--enable-javascript")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")

chrome_options.add_argument(“-headless”)

2 个答案:

答案 0 :(得分:1)

为该按钮btn2分配了一个 id ,它也是唯一的。

您应该通过链接文字优先使用 id

也就是说,视点中没有下一页链接。为此,您首先必须像这样移动驱动程序的焦点:

wait = WebDriverWait(self.browser,10)
next_page = wait.until(EC.visibility_of_element_located((By.ID, "btn2")))
ActionChains(self.browser).move_to_element(next_page).perform()
next_page.click()  

导入:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 

答案 1 :(得分:-1)

所需元素是启用了JavaScript的元素,因此必须在元素上定位click()并为element_to_be_clickable()引入 WebDriverWait ,然后可以使用以下任一Locator Strategies

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.botons[id^='btn2'] i.fas.fa-long-arrow-alt-right"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='botons' and normalize-space()='Next Page']/i[@class='fas fa-long-arrow-alt-right']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
相关问题