[英]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:我想自动化我的打开 session 所以,按照 Stackoverflow 上的一些教程和其他问题,我做了以下事情:
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:我认为对应的 html 将是:
<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
. Submit 按钮上有一个重叠的元素,因此您可以使用其 xpath 单击该元素,对于用户名和密码字段,您应该使用id
而不是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 .提交按钮Se 连接器与通知重叠。 You can Scroll up the element inducing WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies :您可以向上滚动元素诱导WebDriverWait的element_to_be_clickable()
并且您可以使用以下任一定位器策略:
Using CSS_SELECTOR
:使用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
:使用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";最后,如果我将“find_element_by_name”都替换为“find_element_by_id”,代码会更好地工作; but within Safari, it won't seem to click.但在 Safari 内,它似乎不会点击。 I instead used Chrome(), but in that case I needed to download 'chromedriver' and indicate the path where it is:我改为使用 Chrome(),但在这种情况下,我需要下载“chromedriver”并指出它所在的路径:
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...)然后它工作得很好(但网站要求完成验证码,所以我想这有点像自动化之路的尽头......)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.