Home > Back-end >  Trying to click on Metamask Login Button and Nothing Happens - Selenium Python
Trying to click on Metamask Login Button and Nothing Happens - Selenium Python

Time:09-27

I want to create a scraping program on the PvU site https://marketplace.plantvsundead.com/#/login

I've managed to open Chrome with the Metamask extension and to log in to metamask.

Now I need to login on to the PvU platform, but I can't click on the "Log in with MetaMask" button.

I've tried to click with:

1 -

driver.find_element(By.CSS_SELECTOR,"div.metamask.tw-flex.tw-items-center.tw-p-3.tw-cursor-pointer").click()

2 -

driver.find_element(By.XPATH, '//*[@id="__layout"]/div/div[2]/div/div[2]/div[1]/img')

3 -

element = WebDriverWait(driver, delay).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "div.metamask.tw-flex.tw-items-center.tw-p-3.tw-cursor-pointer")))
ac.move_to_element(element).click(element).perform()

No errors, but nothing happens.

Does anyone know what to do?

My code:

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import sys
from selenium.webdriver.common.keys import Keys
from time import sleep
import asyncio
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
 
def run_chrome():
    
    #criando uma instância do chrome com o profile escolhido    
    options = Options()
    options.add_argument("start-maximized")
    options.add_argument("user-data-dir=C:\\Python")
    driver = webdriver.Chrome(options=options)
    ac = ActionChains(driver)
    
    #abrindo o metamask e logando
    driver.get('chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/home.html')
    delay = 3 # seconds
    try:
        WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, '//*[@id="password"]')))
        print ("Page is ready!")
    except TimeoutException:
        print ("Loading took too much time!")
    s_pass = "PASSWORD"
    driver.find_element(By.XPATH, '//*[@id="password"]').send_keys(s_pass)
    driver.find_element(By.XPATH, '//*[@id="app-content"]/div/div[4]/div/div/button/span').click()
    
    #abrindo uma nova guia com o metamaks
    EXTENSION_ID = 'nkbihfbeogaeaoehlefnkodbefgpgknn'
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[1])
    driver.get('chrome-extension://{}/popup.html'.format(EXTENSION_ID))
    
    
    
    #abrindo o PvU
    driver.switch_to.window(driver.window_handles[0])
    driver.get('https://marketplace.plantvsundead.com/#/login')
    
    #I CAN'T PASS THIS 

    try:
        WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, '//*[@id="__layout"]/div/div[2]/div/div[2]/div[1]/img')))
        print ("Page is ready!")
    except TimeoutException:
        print ("Loading took too much time!")
    try:
        content = driver.find_element(By.CSS_SELECTOR,"div.metamask.tw-flex.tw-items-center.tw-p-3.tw-cursor-pointer")
        print ("Page is ready!")
    except TimeoutException:
        print ("Loading took too much time!")
    content.click()
    element = WebDriverWait(driver, delay).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "div.metamask.tw-flex.tw-items-center.tw-p-3.tw-cursor-pointer")))
    ac.move_to_element(element).click(element).perform()
    
    time.sleep(2)
    driver.switch_to.window(driver.window_handles[1])
    time.sleep(2)
    
run_chrome()

THANK YOU

CodePudding user response:

Try this one, this will work for you:

ele=driver.find_element_by_xpath("//*[@id='__layout']//p[starts-with(normalize-space(),'Log in with MetaMask')]")
ele.click()

CodePudding user response:

Tried this locally and the click worked for me.

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

driver = webdriver.Chrome()
driver.get("https://marketplace.plantvsundead.com/#/login")

item = WebDriverWait(driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, '//div[contains(@class, "metamask tw-flex tw-items-center tw-p-3")]')))
time.sleep(0.15)
item.click()

But I got a popup saying 'Please install metamask'. Have a look here to solve that issue. https://dev.to/ltmenezes/automated-dapps-scrapping-with-selenium-and-metamask-2ae9

  • Related