Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NoSuchElementException:消息:没有这样的元素:使用Selenium和Python刮取前20个支架时无法定位元素错误_Python_Selenium_Xpath_Webdriverwait_Expected Condition - Fatal编程技术网

NoSuchElementException:消息:没有这样的元素:使用Selenium和Python刮取前20个支架时无法定位元素错误

NoSuchElementException:消息:没有这样的元素:使用Selenium和Python刮取前20个支架时无法定位元素错误,python,selenium,xpath,webdriverwait,expected-condition,Python,Selenium,Xpath,Webdriverwait,Expected Condition,我试着刮去ERC-20链条上最上面的20个令牌持有者。我用硒。 xpath似乎没有加载/没有足够的时间 我尝试加载此页面: 我尝试了隐式等待和显式等待。我甚至可以看到,当我运行webdriver时,该端正在加载,但它从未找到路径 带有显式等待的代码: options = Options() ptions.add_argument("--disable-dev-shm-using") options.add_argument("--no-sandbox")

我试着刮去ERC-20链条上最上面的20个令牌持有者。我用硒。 xpath似乎没有加载/没有足够的时间

我尝试加载此页面:

我尝试了隐式等待和显式等待。我甚至可以看到,当我运行webdriver时,该端正在加载,但它从未找到路径

带有显式等待的代码:

options = Options()
ptions.add_argument("--disable-dev-shm-using")
options.add_argument("--no-sandbox")
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7#balances")
wait = WebDriverWait(driver, 10, poll_frequency=1)
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="maintable"]/div[3]/table/tbody/')))
错误:

selenium.common.exceptions.TimeoutException: Message:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="maintable"]/div[3]/table/tbody/tr[1]/td[2]/span/a"}
是的,甚至没有留言

带有隐式的代码:

options = Options()
ptions.add_argument("--disable-dev-shm-using")
options.add_argument("--no-sandbox")
driver = webdriver.Chrome(chrome_options=options)
driver.implicitly_wait(10)
driver.get("https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7#balances")
for i in range(1,20):
            req = driver.find_element_by_xpath('//*[@id="maintable"]/div[3]/table/tbody/tr['+str(i)+']/td[2]/span/a')
            
错误:

selenium.common.exceptions.TimeoutException: Message:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="maintable"]/div[3]/table/tbody/tr[1]/td[2]/span/a"}
所以就像我说的,看起来驱动程序没有足够的时间加载页面,但即使有20,30,。。。他们找不到路


另外,当我从脚本打开的浏览器复制xpath时,我可以找到xpath。

该表位于
iframe
中,您需要先切换到iframe才能访问该表

诱导
WebDriverWait
()并等待
frame\u可用,然后切换到\u it
()

导入
WebDriverWait
()并等待所定位的所有元素的
可见性()

代码

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

driver=webdriver.Chrome()
driver.get("https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7#balances")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"tokeholdersiframe")))
elements=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,'//*[@id="maintable"]/div[3]/table/tbody//tr/td[2]//a')))
for ele in elements:
    print(ele.get_attribute('href'))

如果要获取前20个令牌,请使用此令牌

for ele in elements[:20]:
    print(ele.get_attribute('href'))

该表位于
iframe
中,您需要先切换到iframe才能访问该表

诱导
WebDriverWait
()并等待
frame\u可用,然后切换到\u it
()

导入
WebDriverWait
()并等待所定位的所有元素的
可见性()

代码

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

driver=webdriver.Chrome()
driver.get("https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7#balances")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"tokeholdersiframe")))
elements=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,'//*[@id="maintable"]/div[3]/table/tbody//tr/td[2]//a')))
for ele in elements:
    print(ele.get_attribute('href'))

如果要获取前20个令牌,请使用此令牌

for ele in elements[:20]:
    print(ele.get_attribute('href'))

由于持有人信息在
中,因此要刮取ERC-20链上的前20名令牌持有人,您必须:

  • 滚动查看
    令牌持有者图表

  • 诱导WebDriverWait以使所需帧可用并切换到该帧

  • 诱导WebDriverWait以获得所需的

  • 您可以基于以下内容使用:

  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 控制台输出:

    ['0x7b8c69a0f660cd43ef67948976daae77bc6a019b', 'Binance 7', '0x5754284f345afc66a98fbb0a0afe71e0f007b949', 'Binance', 'Huobi 9', 'Bittrex 3', '0xd545f6eaf71b8e54af1f02dafba6c0d46c491cc1', '0x778476d4c51f93078d61e51c978f90b4a6e500af', 'Bitfinex 2', '0x5041ed759dd4afc3a72b8192c143f72f4724081a', '0xd30b438df65f4f788563b2b3611bd6059bff4ad9', '0x570aeda18a21d8fff6d28a5ef34164553cf9cb77', '0x2b9dc5aaf7b1c15f1fd8aba255919c2a7a184453', '0x6a5b1111a0b5ea8c7ec5665ba09cbacd7fde2b96', 'Gate.io 1', '0x9ec7d40d627ec59981446a6e5acb33d51afcaf8a', '0x231568baa78111377f097bb087241f8379fa18f4', '0xd33547964bae70e1ddd2863a4770dc5cffd86269', 'Huobi 3', 'Compound Tether']
    

由于持有人信息在
中,因此您必须:

  • 滚动查看
    令牌持有者图表

  • 诱导WebDriverWait以使所需帧可用并切换到该帧

  • 诱导WebDriverWait以获得所需的

  • 您可以基于以下内容使用:

  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 控制台输出:

    ['0x7b8c69a0f660cd43ef67948976daae77bc6a019b', 'Binance 7', '0x5754284f345afc66a98fbb0a0afe71e0f007b949', 'Binance', 'Huobi 9', 'Bittrex 3', '0xd545f6eaf71b8e54af1f02dafba6c0d46c491cc1', '0x778476d4c51f93078d61e51c978f90b4a6e500af', 'Bitfinex 2', '0x5041ed759dd4afc3a72b8192c143f72f4724081a', '0xd30b438df65f4f788563b2b3611bd6059bff4ad9', '0x570aeda18a21d8fff6d28a5ef34164553cf9cb77', '0x2b9dc5aaf7b1c15f1fd8aba255919c2a7a184453', '0x6a5b1111a0b5ea8c7ec5665ba09cbacd7fde2b96', 'Gate.io 1', '0x9ec7d40d627ec59981446a6e5acb33d51afcaf8a', '0x231568baa78111377f097bb087241f8379fa18f4', '0xd33547964bae70e1ddd2863a4770dc5cffd86269', 'Huobi 3', 'Compound Tether']
    

您好,谢谢您,但是由于代码:selenium.common.exceptions.NoSuchWindowException:Message:没有这样的窗口,只有另一个答案works@MToken我的代码中没有窗口切换,您无论如何都不应该看到
NoSuchWindowException
。不断更新你的库。我能说什么。。。当我有两次驱动程序时。获取(url)。。。在我的代码中,它也可以与您一起使用解决方案。。。但如果没有:selenium.common.exceptions.NoSuchWindowException:消息:没有这样的window@MToken您还没有详细地告诉我们您的用例,但是我建议您不要使用
--无沙盒
--使用
禁用dev shm,除非必要。这些在不同的情况下使用。您好,谢谢您,但是由于代码:selenium.common.exceptions.NoSuchWindowException:Message:没有这样的窗口,但是另一个答案是works@MToken我的代码中没有窗口切换,您无论如何都不应该看到
NoSuchWindowException
。不断更新你的库。我能说什么。。。当我有两次驱动程序时。获取(url)。。。在我的代码中,它也可以与您一起使用解决方案。。。但如果没有:selenium.common.exceptions.NoSuchWindowException:消息:没有这样的window@MToken您还没有详细地告诉我们您的用例,但是我建议您不要使用
--无沙盒
--使用
禁用dev shm,除非必要。这些都是在不同的情况下使用的。谢谢你,但另一个非常奇怪的想法发生了。。。如果我两次都没有这行代码:driver.get(“)driver.get(“)我会得到与另一个Ansare相同的错误…:selenium.common.exceptions.NoSuchWindowException:Message:没有这样的窗口谢谢,但另一个非常奇怪的想法发生了。。。如果我没有这一行2次:driver.get(“)driver.get(“)我会得到与另一个Ansare相同的错误…:selenium.common.exceptions.NoSuchWindowException:消息:没有这样的窗口