【问题标题】:Unable to locate element method using find element by Xpath in Selenium?无法在 Selenium 中使用 Xpath 查找元素来定位元素方法?
【发布时间】:2022-01-10 04:09:40
【问题描述】:

我想在“mintMultiple: Input”中输入 0.5。我尝试使用此代码通过 Xpath 查找元素:

import numpy as np
import pandas as pd
from bs4 import BeautifulSoup as soup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time 

driver = webdriver.Chrome(executable_path=r'C:\Users\Main\Documents\Work\Projects\Scraping Websites\extra\chromedriver')

my_url = "https://etherscan.io/address/0x6eed5b7ec85a802428f7a951d6cc1523181c776a#writeContract"

driver.get(my_url)
time.sleep(2)
your_input = driver.find_element_by_xpath(r'//input[@id="input_payable_3_mintMultiple"]')

但是,我得到了这个错误,我尝试使用延迟而不是使用其他来源建议的延迟,但我得到了同样的错误。

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@id="input_payable_3_mintMultiple"]"}
  (Session info: chrome=96.0.4664.45)

这表明 Xpath 是正确的,因为它在 HTML 中以黄色突出显示。

https://etherscan.io/address/0x6eed5b7ec85a802428f7a951d6cc1523181c776a#writeContract

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping xpath


    【解决方案1】:

    这是因为 输入隐藏在下拉选项卡中,并且不会显示在 HTML 下。为了使元素可交互,您必须首先单击下拉选项卡。在“your_input”变量之前添加以下代码行:

    driver.find_element_by_xpath("//*[@id='heading3']/a").click() #dropdown tab
    

    进一步完成代码:

    your_input.click()
    your_input.send_keys("0.5")
    

    【讨论】:

    • 我收到此错误 'NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="heading3"]/ a"}(会话信息:chrome=96.0.4664.45)'
    • @user13174343 刚刚看到我的错误!将“heading3”更改为“heading3”
    • 错误仍然存​​在
    • @user13174343 正在处理它
    【解决方案2】:

    元素在<iframe> 内。所以你必须切换到框架,你可以使用以下Locator Strategies

    driver.get("https://etherscan.io/address/0x6eed5b7ec85a802428f7a951d6cc1523181c776a#writeContract")
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#btnCookie"))).click()
    WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#writecontractiframe")))
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.LINK_TEXT, "3. mintMultiple"))).click()
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#input_payable_3_mintMultiple"))).send_keys("0.5")
    
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
    • 浏览器快照:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-05
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 2018-10-31
      相关资源
      最近更新 更多