简体   繁体   中英

Unable to locate element method using find element by Xpath in Selenium?

I want to type 0.5 into the "mintMultiple: Input". I have tried finding element by Xpath using this code:

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"]')

However, I get this Error, I've tried using a delay and not using a delay as suggested by other sources, but I get the same error.

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

This shows the Xpath is correct as it highlighted in yellow in the HTML.

https://etherscan.io/address/0x6eed5b7ec85a802428f7a951d6cc1523181c776a#writeContract 在此处输入图像描述

This is because the input is hidden within the drop-down tab and does not show up under the HTML. In order for the element to be interactable, you must first click on the drop down tab. Add the following line of code before "your_input" variable:

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

Furthermore to complete the code:

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

The element is within an iframe . So you have to switch to the frame and you can use the following 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")
  • Note : You have to add the following imports:

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
  • Browser Snapshot:

薄荷多

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM