python
python

Reputation: 4521

Clicking is not working in python selenium

I am trying to create a bot which will fill up the signup forms for a particular website.

Website Details - http://hacknyu.org/signup

Code:

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('[email protected]')
    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()

Problem

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

The main problem is in this line. Manually when you click on signup button it is working fine but when I run this program, I can see it is clicking on signup button but nothing is happening after that. Could anyone help me why this is not working? I am just trying to register for an event using a bot. I will highly appreciate any help you can provide.

Error

Sometimes I always receive this error

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

Upvotes: 3

Views: 6081

Answers (3)

Florent B.
Florent B.

Reputation: 42518

The page has a banner at the top which makes the automatic scrolling hide the submit button. To overcome this issue, you can define the scrolling behaviour to scroll at the bottom instead. Moreover it seems that your script is no correctly clicking on the checkbox that should display the terms popup.

Here is a working script to create a new account on 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("[email protected]")
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()

Upvotes: 1

murali selenium
murali selenium

Reputation: 3927

Mostly providing wait or sleep before click will helps you simulate click correctly. Some situations click by using Actions also helps me. In Java

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

and one more way is to use javascript executor.

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

Thank You, Murali

Upvotes: 1

Guy
Guy

Reputation: 50809

It is probably timing issue. You can use explicit wait to make sure the element is clickable

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

This will wait up to 10 seconds for the button to be clickable before clicking on it.

Upvotes: 1

Related Questions