Home > Mobile >  function click with selenium without id
function click with selenium without id

Time:01-10

I want know how I can click a button in this page here with selenium the code I try is this

import selenium 
from selenium import webdriver
from time import sleep
PATH= "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://yopmail.com/it/")
inputsSI= driver.find_element_by_class_name("md").click()
print(inputsSI)
sleep(100)
driver.close()

the error I get is this:

Message: element not interactable

CodePudding user response:

It's because find_element_* methods will return the first occurrence of the element they find. If you see the HTML page there is another div element before the button that has this "md" class and of course it is not the button you are looking for.

You are looking for this buttun :

<div id="refreshbut">
    <button  style="border-radius: 20px;"
            title="Controllare la posta @yopmail.com"
            onclick="{if(chkl())go()}"><i
            >&#xe5c8;</i></button>
    <input type="submit" style="display:none;">
</div>

It is wrapped inside a div with id of "refreshbut".

So all you have to do is first get this div by id. Then search for the element which has the "md" class which is indeed the button you are looking for. (you could also get the button with XPATH. it's up to you)

like:

inputsSI = driver.find_element(By.ID, "refreshbut") \
    .find_element(By.CLASS_NAME, "md") \
    .click()

Note 1: It's better to use raw-string for your PATH variable.

Note 2: I would put the driver.close() statement inside the finally block so that it always runs

CodePudding user response:

To click on the md button use button.md as a selector then wait for the element to be interactable. I don't know what goes in there so I just added a send keys of a.

wait=WebDriverWait(driver,30)                                 
driver.get("https://yopmail.com/it/")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#login"))).send_keys('a')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button.md"))).click()

Imports:

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