简体   繁体   English

关于使用 Selenium 和 Python 点击链接的问题

[英]Problem about link clicking with Selenium and Python

I need your help... this is getting me crazy:我需要你的帮助......这让我抓狂:

mydriver = webdriver.Chrome()
reg_url="https://etherscan.io/token/0x85f0e02cb992aa1f9f47112f815f519ef1a59e2da=0x0000000000000000000000000000000000000000"

mydriver.get(reg_url)
iframe = mydriver.find_element_by_xpath("//iframe[@id='tokentxnsiframe']") 
mydriver.switch_to.frame(iframe)

count_added = 0

while cond : 

    elems = mydriver.find_elements_by_xpath('//*[@id="maindiv"]/div[2]/table/tbody/tr/td[8]/a')

    #DOING THING WITH THE elems
mydriver.execute_script("window.scrollTo(0, document.body.scrollHeight)") 


    time.sleep(5)

    element = mydriver.find_element_by_xpath('//*[@id="maindiv"]/div[1]/nav/ul/li[4]/a')

    actions = webdriver.ActionChains(mydriver)
    actions.move_to_element(element).perform()
    time.sleep(5)
    actions.click().perform()

    time.sleep(5)

Page HTML is:页 HTML 是:

<div class="d-md-flex justify-content-between mb-4">
   <p class="mb-2 mb-md-0">
      <i id="spinwheel" class="fa fa-spin fa-spinner fa-1x fa-pulse mr-1" style="display: none;"></i>
      <i class="fa fa-sort-amount-desc mr-1" rel="tooltip" data-toggle="tooltip" title="" data-original-title="Oldest First"></i>A total of 9,725 transactions found
   </p>
   <nav aria-label="page navigation">
      <ul class="pagination pagination-sm mb-0">
         <li class="page-item disabled"><span class="page-link">First</span></li>
         <li class="page-item disabled"><span class="page-link"><i class="fa fa-chevron-left small"></i></span><span class="sr-only">Previous</span></li>
         <li class="page-item disabled"><span class="page-link text-nowrap">Page <strong class="font-weight-medium">1</strong> of <strong class="font-weight-medium">389</strong></span></li>
         <li class="page-item" data-toggle="tooltip" title="" data-original-title="Go to Next"><a class="page-link" href="javascript:move('generic-tokentxns2?contractAddress=0x85f0e02cb992aa1f9f47112f815f519ef1a59e2d&amp;mode=&amp;sid=4b6ef2c27b590c4f6cd4bef249b5ced6&amp;a=0x0000000000000000000000000000000000000000&amp;m=normal&amp;p=2')" aria-label="Next"><span aria-hidden="True"><i class="fa fa-chevron-right small"></i></span> <span class="sr-only">Next</span></a></li>
         <li class="page-item"><a class="page-link" href="javascript:move('generic-tokentxns2?contractAddress=0x85f0e02cb992aa1f9f47112f815f519ef1a59e2d&amp;mode=&amp;sid=4b6ef2c27b590c4f6cd4bef249b5ced6&amp;a=0x0000000000000000000000000000000000000000&amp;m=normal&amp;p=389')"><span aria-hidden="True">Last</span> <span class="sr-only">Last</span></a></li>
      </ul>
   </nav>
</div>

Basically I want the script to click on the next page link on the frame to go to the next page.基本上我希望脚本单击框架上的下一页链接到 go 到下一页。 Instead of going there, it's clicking to some random link.它没有去那里,而是点击一些随机链接。 I've tried to get more specific on finding the link with mydriver.find_element_by_xpath('//a[@area-label="Next"]') but I get the same result.我试图更具体地找到与mydriver.find_element_by_xpath('//a[@area-label="Next"]')的链接,但我得到了相同的结果。

Try to use xpath like that:尝试像这样使用 xpath :

driver.find_element_by_xpath("(//a[@class='page-link' and @aria-label='Next'])[1]")

Try css selector below:试试下面的 css 选择器:

button = driver.find_element_by_css_selector("nav li[data-original-title="Go to Next"]>.page-link")
wait = WebDriverWait(mydriver, 10)
mydriver.get("https://etherscan.io/token/0x85f0e02cb992aa1f9f47112f815f519ef1a59e2d?a=0x0000000000000000000000000000000000000000")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#btnCookie"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it("tokentxnsiframe"))
wait.until(EC.element_to_be_clickable((By.XPATH,"//a[@aria-label='Next']"))).click()

You should remove the element on top first and then switch to the iframe and click the next button.Also start using webdriver wait to ensure the stability of your programs instead of time.sleep().您应该先删除顶部的元素,然后切换到 iframe 并单击下一步按钮。同时开始使用 webdriver wait 来确保程序的稳定性,而不是 time.sleep()。

Import进口

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

Outputs输出

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM