How do I properly click an element in Selenium with Python? - python

I am trying to click an element, for example "AH", in this page. I use the following code.
from selenium import webdriver
url = "http://www.oddsportal.com/soccer/brazil/serie-a/internacional-santos-vcGTTAKH/"
driver = webdriver.Firefox()
driver.get(url)
element_to_click = driver.find_element_by_link_text("AH")
element_to_click.click()
The problem is that after the element is clicked and the new page is loaded, it goes back to the first page.

Focus the element and call click_and_hold action (worked for me):
from selenium.webdriver import ActionChains
actions = ActionChains(driver)
actions.move_to_element(element_to_click).click_and_hold(element_to_click).perform()

alecxe , that works.
Just to add to the discussion here
So on mouse down it is invoking onClick for the uid(4), when we do a normal click on the element we do not realize that it worked on mouse down not on mouse click.
Thats why when we are using webdriver to do element.click() on it, this does not work and when we use Actions class to simulate mouse down using click_and_hold, It works !!

Related

How do I click a particular element in a pop up?

I have written the following selenium script:
from selenium import webdriver
PATH= r"C:\Users\David\Desktop\Selenium\chromedriver.exe"
driver=webdriver.Chrome(PATH)
driver.get("https://www.studentbeans.com/uk")
When I enter the website, there is a pop up that appears that asks if I'd like to accept all cookies. I would like to click yes. How do I add this onto my code?
Firstly, you have to select the cookie element using xpath or css selector then click by calling click() function.Remember it that you also need to make full screen using driver.maximize_window()
Try:
import time
from selenium import webdriver
PATH= r"C:\Users\David\Desktop\Selenium\chromedriver.exe"
driver=webdriver.Chrome(PATH)
driver.get("https://www.studentbeans.com/uk")
driver.maximize_window()
time.sleep(4)
cookie_button=driver.find_element_by_xpath('//button[#id="onetrust-accept-btn-handler"]').click()
time.sleep(2)
Inspect the HTML, and find that element. Then copy the xpath or css selector or whatever and:
driver.find_element_by_xpath('copy xpath').click()

Click a button using Selenium on python

I am trying to click this button to export my data as a csv file. I'm using Selenium on python.
I can not figure out how to press the button because there is no id or name and the link changes every time.
Thanks in advance!
You have to scroll to that element. Like this:
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_xpath("//a[contains(#href,'swissadme.csv')]")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
element.click()
If the locator above not enough try this instead:
"//a[contains(#href,'results/50881847/swissadme.csv')]"
See if this works:-
elm = driver.find_element_by_xpath(".//div[contains(text(),'Retrieve date:')]/a[contains(#href,''swissadme.csv)]")
driver.execute_script("arguments[0].scrollIntoView(true);",elm);
elm.click()

ActionChains in Selenium not clicking button (Python)

I'm trying to use ActionChains to click a button with
python but it just refuses to work no matter what I do.
The issue is that whenever the website opens, it opens with an overlay.
I want my program to click the 'OK' button on the overlay. Whatever code I write just ends up clicking the overlay itself.
Here is my code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
URL = 'https://sam.gov/search/?index=opp&page=1&sort=-relevance&sfm%5Bstatus%5D%5Bis_active%5D=true&sfm%5Bdates%5D%5BresponseDue%5D%5BresponseDueSelect%5D=customDate&sfm%5Bdates%5D%5BresponseDue%5D%5BresponseDueFrom%5D=05%2F29%2F2021&sfm%5Bdates%5D%5BresponseDue%5D%5BresponseDueTo%5D=05%2F29%2F2022&sfm%5Bkeywords%5D%5B0%5D%5Bkey%5D=541511&sfm%5Bkeywords%5D%5B0%5D%5Bvalue%5D=541511'
driver.get(URL)
overlay = driver.find_element(By.ID, "cdk-overlay-0")
button = driver.find_element(By.CSS_SELECTOR, "button.usa-button")
ActionChains(driver).move_to_element(overlay).click(button).perform()
And the relevant HTML from the webpage I'm looking at is:
<div id="cdk-overlay-0" class="cdk-overlay-pane" ...>
for the overlay, and
<button class="usa-button">OK</button>
for the button itself.
My code always ends up clicking just on the overlay and not the button. It ends up looking like this (the overlay gets a blue outline when clicked):
Yo, try this
from selenium import webdriver
URL = 'https://sam.gov/search/?index=opp&page=1&sort=-relevance&sfm%5Bstatus%5D%5Bis_active%5D=true&sfm%5Bdates%5D%5BresponseDue%5D%5BresponseDueSelect%5D=customDate&sfm%5Bdates%5D%5BresponseDue%5D%5BresponseDueFrom%5D=05%2F29%2F2021&sfm%5Bdates%5D%5BresponseDue%5D%5BresponseDueTo%5D=05%2F29%2F2022&sfm%5Bkeywords%5D%5B0%5D%5Bkey%5D=541511&sfm%5Bkeywords%5D%5B0%5D%5Bvalue%5D=541511'
driver = webdriver.Chrome()
driver.get(URL)
driver.find_element_by_xpath(r'/html/body/div/div[2]/div/sds-dialog-container/layout-splash-modal/div[4]/div[2]/div/button').click()
I just used the xpath and clicked it without actionchains

Cannot click on an xpath selected object Selenium (Python)

I am trying to click to an object that I select with Xpath, but there seems to be problem that I could not located the element. I am trying to click accept on the page's "Terms of Use" button. The code I have written is as
driver.get(link)
accept_button = driver.find_element_by_xpath('//*[#id="terms-ok"]')
accept_button.click()
prov = driver.find_element_by_id("province-region")
prov.click()
Here is the HTML code I have:
And I am getting a "NoSuchElementException". My goal is to click this "Kabul Ediyorum" button at the bottom of the HTML code. I started to think that we have some restrictions on the tasks we could do on that page. Any ideas ?
Not really sure what the issue might be.
But you could try the following:
Try to locate the element by its visible text
accept_button = driver.find_element_by_xpath("//*[text()='Kabul Ediyorum']").click()
Try with ActionChains
For that you need to import ActionChains
from selenium.webdriver.common.action_chains import ActionChains
accept_button = driver.find_element_by_xpath("//*[text()='Kabul Ediyorum']")
actions = ActionChains(driver)
actions.click(on_element=accept_button).perform()
Also make sure you have implicitly wait
# implicitly wait
driver.implicitly_wait(10)
Or explicit wait
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[text()='Kabul Ediyorum']"))).click()
Hope this helped!

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('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()
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
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("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()
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.
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

Categories