简体   繁体   中英

How to locate and click on the submit button on codeur.com signin page using Selenium and Python

I want to automate my opening session so, following some tutorials and other questions on Stackoverflow, I did the following:

import selenium  

from selenium import webdriver

driver = webdriver.Safari()


driver.get('https://www.codeur.com/users/sign_in')


id_box = driver.find_element_by_name('user_email')

id_box.send_keys(my_mail)

# Find password box
pass_box = driver.find_element_by_name('user_password')
# Send password
pass_box.send_keys(my_password)
# Find login button
login_button = driver.find_element_by_name('commit')
# Click login
login_button.click()

I get the following error:

line 321, in execute
    self.error_handler.check_response(response)
line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: 


Process finished with exit code 1

Is it because I missed the right name to the submit button?

The html corresponding, I think, would be:

<span class="recaptcha-button-enabled">
<input type="submit" name="commit" value="Se connecter" class="btn btn-primary btn-lg btn-block" data-disable-with="Se connecter">
</span>
<span class="recaptcha-button-disabled" style="display: none">
<input type="submit" name="commit" value="Connexion en cours…" class="btn btn-primary btn-lg btn-block" disabled="disabled" data-disable-with="Connexion en cours…">
</span>

There is an overlapping element on the Submit button, so you can click on that element using its xpath and for user name and password fields, you should use id to fetch the element instead of name .

Your code should be like:

import selenium  

from selenium import webdriver

driver = webdriver.Safari()


driver.get('https://www.codeur.com/users/sign_in')


id_box = driver.find_element_by_id("user_email")

id_box.send_keys(my_mail)

# Find password box
pass_box = driver.find_element_by_id("user_password")
# Send password
pass_box.send_keys(my_password)
# Find login button
login_button = driver.find_element_by_xpath("//span[@class='recaptcha-button-enabled']")
# Click login
login_button.click()

However, there is a captcha getting displayed after clicking on Submit button on the page so I don't think you would be able to proceed with the automation.

The submit button Se connecter is overlapped by a notification . You can Scroll up the element inducing WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :

  • Using CSS_SELECTOR :

     driver.get('https://www.codeur.com/users/sign_in') WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#user_email"))).send_keys("Netchaiev@gmail.com") driver.find_element_by_css_selector("input#user_password").send_keys("Netchaiev") driver.execute_script("return arguments[0].scrollIntoView(true);", driver.find_element_by_css_selector("input[value='Se connecter']")) driver.find_element_by_css_selector("input[value='Se connecter']").click()
  • Using XPATH :

     driver.get('https://www.codeur.com/users/sign_in') WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='user_email']"))).send_keys("Netchaiev@gmail.com") driver.find_element_by_xpath("//input[@id='user_password']").send_keys("Netchaiev") driver.execute_script("return arguments[0].scrollIntoView(true);", driver.find_element_by_xpath("//input[@value='Se connecter']")) driver.find_element_by_xpath("//input[@value='Se connecter']").click()
  • 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:

编码器

Use like name:

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

element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.NAME, "commit")))

element.click();

Finally the code is working better if I replace both "find_element_by_name" by "find_element_by_id"; but within Safari, it won't seem to click. I instead used Chrome(), but in that case I needed to download 'chromedriver' and indicate the path where it is:

driver = webdriver.Chrome(executable_path='path_to_the_folder_where_chromedriver_is/chromedriver')

And then it worked fine (but the website ask then to fulfill a Captcha, so I guess it is kinda the end of the road for automation...)

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