如何使用Python单击未签名列表中的项

2024-06-10 14:03:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个有按钮的网页 第一,上一,1,2,3,4,5,下一,最后

我想自动单击“下一步”(和其他),但我无法从未签名的列表中找到正确的方法。你知道吗

我可以导航到列表中的特定项。但是,如果不返回错误,则无法对该项执行.click()。你知道吗

这是html代码的一部分

<nav>
    <input type="hidden" id="page" name="page" value="2">
    <input type="hidden" id="resetPage" name="resetPage" value="">
    <input type="hidden" id="info_firstPage" value="/info#PCB">
    <ul id="info_pageclass" class="pageclass">
        <li class="disabled" style="pointer-events: none;"><a href="/info#PCB"><span>First</span></a></li>,
        <li class="disabled" style="pointer-events: none;"><a href="/info/page0#PCB" rel="prev"><span>Prev</span></a></li>,
        <li class="hidden-xs" style="pointer-events: none;"><a href="/info" style="background: #41ace2 !important; color: #fff !important;">1</a></li>,
        <li class="hidden-xs"><a href="/info/page2#PCB">2</a></li>,
        <li class="hidden-xs"><a href="/info/page3#PCB">3</a></li>,
        <li class="hidden-xs"><a href="/info/page4#PCB">4</a></li>,
        <li class="hidden-xs"><a href="/info/page5#PCB">5</a></li>,
        <li><a href="/info/page2#PCB" rel="next"><span>Next</span></a></li>,
        <li><a href="/info/page9#PCB"><span>Last</span></a></li>
    </ul>
</nav>

这是python代码与IDE一起单步执行

>>> soup = BeautifulSoup(driver.page_source, features='html.parser')
>>> next = soup.find("ul", attrs={"id":"info_pageclass"}).find_all("li")[7]
>>> print(next)
<li>
<a href="/info/page2#PCB" rel="next">
<span>Next</span>
</a>
</li>

这些语句都返回与所示相同的错误(一次)

>>> next.send_keys(Keys.ENTER)
>>> next.click()
>>> next.a.click()
TypeError: 'NoneType' object is not callable

Tags: infoidinputstyletypepagelihidden
2条回答

click()在文本为Next的元素上,您需要诱导WebDriverWait,使元素可单击,并且可以使用以下任一解决方案:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.pageclass#info_pageclass li>a[rel='next']>span"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='pageclass' and @id='info_pageclass']//li/a[@rel='next']/span[text()='Next']"))).click()
    
  • 注意:必须添加以下导入:

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

使用SeleniumBeautifulSoup。使用infinite while loop并搜索“下一步”按钮元素。但是当页面上没有next button元素时,此操作将中断。你知道吗

from selenium import webdriver
from bs4 import BeautifulSoup

url = 'Url here'
driver = webdriver.Chrome()
driver.get(url)
soup = BeautifulSoup(driver.page_source,"html.parser")

#While loop search for the `Next` button on the page if not found jumps out from the loop.
while True:
    #Some code of your operation

    if len(driver.find_elements_by_xpath('//ul[@id="info_pageclass"]//li/a/span[contains(.,"Next")]'))>0:
        driver.find_element_by_xpath('//ul[@id="info_pageclass"]//li/a/span[contains(.,"Next")]').click()
        soup = BeautifulSoup(driver.page_source,"html.parser")
    else:
        break

相关问题 更多 >