我正试图在Python中使用Selenium,我试图通过点击增加数字按钮来遍历bigkinds.or.kr上的登陆页面。
根据Chrome检查器,下一页位于以下HTML中:
<div class="newsPage">
<div class="btmDelBtn">
...</div>
<span>
<a href="javascript:void(0);" class="current">1</a>
<a href="javascript:void(0);" onclick="getSearchResultNew(2)">2</a>
<a href="javascript:void(0);" onclick="getSearchResultNew(3)">3</a>
<a href="javascript:void(0);" onclick="getSearchResultNew(4)">4</a>
<a href="javascript:void(0);" onclick="getSearchResultNew(5)">5</a>
<a href="javascript:void(0);" onclick="getSearchResultNew(6)">6</a>
</span>
点击下一页,我无法成功抓取。请帮我。 这是我的代码:
url = "https://www.bigkinds.or.kr/main.do"
browser.get(url)
...
currentPageElement = browser.find_element_by_xpath("//*[@id='content']/div/div/div[2]/div[7]/span/a[2]")
print(currentPageElement)
currentPageNumber = int(currentPageElement.text)
print(currentPageNumber)
在xpath中,&#34; / span / a [ 2 ]&#34;是页码。如何为此xpath创建循环。
答案 0 :(得分:1)
尝试使用以下代码:
from selenium.common.exceptions import NoSuchElementException
url = "https://www.bigkinds.or.kr/main.do"
browser.get(url)
page_count = 1
while True:
# Increase page_count value on each iteration on +1
page_count += 1
# Do what you need to do on each page
# Code goes here
try:
# Clicking on "2" on pagination on first iteration, "3" on second...
browser.find_element_by_link_text(str(page_count)).click()
except NoSuchElementException:
# Stop loop if no more page available
break
更新
如果您仍想使用XPath
搜索,则可能需要替换
browser.find_element_by_link_text(str(page_count)).click()
带行
browser.find_element_by_xpath('//a[@onclick="getSearchResultNew(%s)"]' % page_count).click()
...或者如果你想使用你的绝对XPath
(不是最好的主意),你可以试试
browser.find_element_by_xpath("//*[@id='content']/div/div/div[2]/div[7]/span/a[%s]" % page_count).click()