Home > database >  Can't find element by xpath selenium
Can't find element by xpath selenium

Time:10-07

I want to find maximum number of pages i.e 20 but it cant find the element.

code

from functools import total_ordering
from pandas.core.base import DataError
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import csv
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import string
from csv import reader
import code
import pandas

from selenium.webdriver.support.wait import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from selenium.webdriver.common.by import By

driver=webdriver.Firefox()

driver.get("https://etherscan.io/token/0xB8c77482e45F1F44dE1745F52C74426C631bDD52#balances")
wait = WebDriverWait(driver,30)
num=wait.until(EC.element_to_be_clickable((By.XPATH,"//div[@class='d-inline-block']//*[@class=page-link text-nowrap]")))
print(num)

Page: https://etherscan.io/token/0xB8c77482e45F1F44dE1745F52C74426C631bDD52#balances

CodePudding user response:

Try like this :

Need to switch to iframe and then try to find the pagination Elements.

wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"tokeholdersiframe")))
print(driver.find_element_by_xpath("//div[@class='d-inline-block']//strong[2]").text)
driver.switch_to.default_content()
20

CodePudding user response:

The desired element is in iframe, so please switch to it before interaction.

The below code I have tried in firefox :

driver=webdriver.Firefox()
driver.maximize_window()
#driver.implicitly_wait(30)
wait = WebDriverWait(driver, 20)

driver.get("https://etherscan.io/token/0xB8c77482e45F1F44dE1745F52C74426C631bDD52#balances")
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "tokeholdersiframe")))
num = wait.until(EC.visibility_of_element_located((By.XPATH, "(//span[contains(@class,'page-link text-nowrap')]/strong)[2]")))
print(num.get_attribute('innerText'))

Imports :

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

In case you want to print 1 of 20, please use below xpath

//span[contains(@class,'page-link text-nowrap')]
  • Related