【问题标题】:Can't find element by xpath seleniumxpath selenium 找不到元素
【发布时间】:2021-10-07 09:54:28
【问题描述】:

我想找到最大页数(20)
但它找不到元素。 Website link


代码:

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)

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping


    【解决方案1】:

    想要的元素在iframe中,所以请在交互前切换到它。

    我在 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'))
    

    进口:

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

    如果您想打印 20 个中的 1 个,请使用下面的 xpath

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

    【讨论】:

    • 我正在尝试,但我的代码超时。有什么想法吗?
    • 不,那不应该发生。你在使用火狐吗?确保仔细复制我的代码。它不会抛出任何错误。
    • 是的,我会再检查一次。
    • 在我的回答中,我也最大化了屏幕,请确保执行与提到的完全相同的步骤。
    【解决方案2】:

    试试这样:

    需要切换到iframe,然后尝试找到pagination元素。

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

    【讨论】:

    • 知道为什么它会超时吗?
    • @HamzaAhmed - 你想点击Page 1 of 20。如果是,那不是可点击的元素。
    • 并且 xpath //div[@class='d-inline-block']//*[@class=page-link text-nowrap] 没有突出显示所需的元素。通过 - https://automationpanda.com/2019/01/15/web-element-locators-for-test-automation/
    • 我正在尝试获取 20 的值。
    • 等待条件是element_to_be_clickable 因此问题。将其更改为presence_of_element_located
    猜你喜欢
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 2021-03-20
    • 2014-08-19
    • 1970-01-01
    • 2020-12-22
    • 2013-05-21
    相关资源
    最近更新 更多