单击在 python selenium 中不起作用

Clicking is not working in python selenium

我正在尝试创建一个机器人来填写特定网站的注册表单。

网站详细信息 - http://hacknyu.org/signup

代码:

from selenium import webdriver
class HackNNYU(object):
    first_name = 'input[ng-model="credentials.first_name"]'
    last_name = 'input[ng-model="credentials.last_name"]'
    email = '.col-sm-12>input[ng-model="credentials.email"]'
    password = '.col-sm-12>input[ng-model="credentials.password"]'
    agree_checkbox = '.ng-binding>input[ng-model="checkModel"]'
    sign_up_button = 'div>button[type="submit"]'
    accept_button = 'button[ng-click="positive()"]'

def fill_up_hack_nyu():
    driver = webdriver.Firefox()
    driver.get('http://hacknyu.org/signup')
    driver.find_element_by_css_selector(HackNNYU.first_name).send_keys('Friday')
    driver.find_element_by_css_selector(HackNNYU.last_name).send_keys('Night')
    driver.execute_script("window.scrollTo(0, 300);")
    driver.find_element_by_css_selector(HackNNYU.email).send_keys('ade347@gmail.edu')
    driver.find_element_by_css_selector(HackNNYU.password).send_keys('123456')
    driver.find_element_by_css_selector(HackNNYU.agree_checkbox).click()
    driver.find_element_by_css_selector(HackNNYU.accept_button).click()
    # driver.execute_script("window.scrollTo(0, 400);")
    driver.find_element_by_css_selector(HackNNYU.sign_up_button).click()
fill_up_hack_nyu()

问题

driver.find_element_by_css_selector(HackNNYU.sign_up_button).click()

主要问题出在这一行。当您手动单击 signup button 时它工作正常,但是当我 运行 这个程序时,我可以看到它正在单击 signup button 但之后没有任何反应。谁能帮助我为什么这不起作用?我只是想使用机器人注册一个事件。我将非常感谢您提供的任何帮助。

错误

有时我总是收到这个错误

selenium.common.exceptions.WebDriverException: Message: Element is not clickable at point (796.4000244140625, 45.399993896484375). Other element would receive the click

可能是时间问题。您可以使用 explicit wait 来确保元素可点击

wait = WebDriverWait(driver, 10)
wait.until(expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, HackNNYU.sign_up_button))).click()

这将最多等待 10 秒让按钮变为可点击状态,然后再点击它。

大多数情况下,在点击前提供等待或休眠可以帮助您正确模拟点击。某些情况下使用 Actions 单击也对我有帮助。在Java

 Actions moveAndClick=new Actions(driver);
moveAndClick.moveToElement(driver.findElement(By.cssSelector("HackNNYU.sign_up_button"))).click().build().perform();

另一种方法是使用 javascript 执行器。

WebElement element = driver.findElement(By.cssSelector("HackNNYU.sign_up_button"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", element);

谢谢, 穆拉利

该页面顶部有一个横幅,使自动滚动隐藏了提交按钮。要解决此问题,您可以定义滚动行为以改为在底部滚动。此外,您的脚本似乎没有正确单击应显示条款弹出窗口的复选框。

这是在 hacknyu 上创建新帐户的工作脚本:

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

# set the scrolling behavior to down
DesiredCapabilities.FIREFOX["elementScrollBehavior"] = 1

driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)

# load the page
driver.get("http://hacknyu.org/signup")

# get the form element
form = driver.find_element_by_css_selector("form[name='signupForm']")

# fill the fields
form.find_element_by_css_selector("input[name='firstName']").send_keys("myfirstname")
form.find_element_by_css_selector("input[name='lastName']").send_keys("mylastname")
form.find_element_by_css_selector("input[name='email']").send_keys("na@na.na")
form.find_element_by_css_selector("input[name='password']").send_keys("mypassword")

# click and accept terms
form.find_element_by_xpath("//input[@name='terms']/..").click()
wait.until(EC.presence_of_element_located((By.XPATH, "//button[.='Accept']"))).click()
wait.until_not(EC.presence_of_element_located((By.CSS_SELECTOR, ".modal")))

# click on submit
form.find_element_by_css_selector("button[type='submit']").click()