import re
from datetime import datetime as dt
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import time
opt = webdriver.ChromeOptions()
profile_path = r'C:\\Users\\*UserName*\\AppData\\Local\\Google\\Chrome\\UserData'
opt.add_argument('--user-data-dir={}'.format(profile_path)) # profile path
opt.add_argument('--profile-directory={}'.format('DEFAULT')) #profile name
opt.add_argument('--log-level=3')
EXTENSION_PATH = 'MM/metamask.crx'
opt.add_extension(EXTENSION_PATH)
driver = webdriver.Chrome('Driver/chromedriver.exe', chrome_options=opt)
global_dynamicUrl = "https://pulsedao.finance/boardroom"
driver.get(global_dynamicUrl)
driver.find_element_by_class_name("MuiButton-label").click()
driver.find_element_by_xpath("/html/body/div[3]/div[3]/nav/button[1]/span[1]").click()
password="**********"
driver.implicitly_wait(20)
driver.find_element(By.XPATH,('//div[contains(@class, "metamask tw-flex tw-items-center tw-p-3")]')).click()
search_box=driver.find_element(By.XPATH,('//div[contains(@class, "metamask tw-flex tw-items-center tw-p-3")]'))
time.sleep(5)
search_box.send_keys(password)
time.sleep(2)
search_box.send_keys(Keys.ENTER)
Gives the error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="password"]"}
I tried numerous other paths:
#Tried by xpath:
#//*[@id="password"]
#//*[@id="app-content"]/div/div[3]/div/div/form/div/div
#('//input')
#'/*[@id="password"]' -> AttributeError: 'str' object has no attribute 'send_keys'
#'/html/body/div[1]/div/div[3]/div/div/form/div/div/input'
#Tried by ID:
#
# Tried by selector
# #password
#'//div[contains(@class, "metamask tw-flex tw-items-center tw-p-3")]'
I followed this at the beginning: https://dev.to/ltmenezes/automated-dapps-scrapping-with-selenium-and-metamask-2ae9 and didn't work either. I tried checking similar questions here on stack, but with so success.
So after several hours of research I feel clueless. Maybe also because I come from Data Science and I attempt this for the first time, even if I have a basic understanding of python.
CodePudding user response:
Find the password field using its class
driver.find_elements_by_class_name('passwordClass').send_keys("password here")
CodePudding user response:
You are missing waits here.
You should wait for elements to be loaded on the page before accessing them.
The preferred approach is to use Expected Conditions explicit waits.
This should work better:
import re
from datetime import datetime as dt
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import time
opt = webdriver.ChromeOptions()
profile_path = r'C:\\Users\\*UserName*\\AppData\\Local\\Google\\Chrome\\UserData'
opt.add_argument('--user-data-dir={}'.format(profile_path)) # profile path
opt.add_argument('--profile-directory={}'.format('DEFAULT')) #profile name
opt.add_argument('--log-level=3')
EXTENSION_PATH = 'MM/metamask.crx'
opt.add_extension(EXTENSION_PATH)
driver = webdriver.Chrome('Driver/chromedriver.exe', chrome_options=opt)
wait = WebDriverWait(driver, 20)
global_dynamicUrl = "https://pulsedao.finance/boardroom"
driver.get(global_dynamicUrl)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.MuiButton-label"))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Metamask']"))).click()
I can't continue from here since I didn't install the extension you are using here and I don't have your credentials but you can see from here how the thinngs working and continue in the similar way