Andrea Costanzo
Andrea Costanzo

Reputation: 61

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:

<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. 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.

Upvotes: 1

Views: 211

Answers (3)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

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().

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

enter image description here

Upvotes: 0

vitaliis
vitaliis

Reputation: 4212

Try css selector below:

button = driver.find_element_by_css_selector("nav li[data-original-title="Go to Next"]>.page-link")

Upvotes: 0

Vova
Vova

Reputation: 3543

Try to use xpath like that:

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

Upvotes: 1

Related Questions