element.click() on selenium not working (noob needs help) - python

Can a noob get some help here?
Hello guys, this is my first time using python, so bear that in mind.
I'm using selenium to navigate and automatically fill a form on this webpage:
https://sede.administracionespublicas.gob.es/icpco/index.html
is all good except for the last element.click()
Here is the code so far:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.maximize_window()
driver.get("https://sede.administracionespublicas.gob.es/icpco/index.html")
driver.implicitly_wait(10)
dropdownmenu=driver.find_element_by_id("form")
sel=Select(dropdownmenu)
sel.select_by_visible_text("Málaga")
element=driver.find_element_by_id("btnAceptar")
element.click()
dropdownmenu1=driver.find_element_by_id("tramiteGrupo[0]")
sel=Select(dropdownmenu1)
sel.select_by_value("4067")
element=driver.find_element_by_id("btnAceptar")
element.click()
element=driver.find_element_by_id("btnEntrar")
element.click()
element=driver.find_element_by_id("txtIdCitado")
element.send_keys("Y7356001V")
element=driver.find_element_by_id("txtDesCitado")
element.send_keys("Franco Padron Chirinos")
element=driver.find_element_by_id("txtAnnoCitado")
element.send_keys("1985")
dropdownmenu=driver.find_element_by_id("txtPaisNac")
sel=Select(dropdownmenu)
sel.select_by_visible_text("VENEZUELA")
element=driver.find_element_by_id("btnEnviar")
element.click()
element=driver.find_element_by_id("btnEnviar")
element.click()
The problem is the last two rows it doesn't work. I'm pretty sure that finds the element but it doesn't activate the click.
Maaaaybe someone can run the script and tell me what I'm doing wrong here?
Thank you!!!!!

You should add a wait before clicking the element. For example, instead of:
element=driver.find_element_by_id("btnEnviar")
element.click()
Try
enviar=driver.find_element_by_id("btnEnviar")
WebDriverWait(driver, 5).until(EC.element_to_be_clickable(enviar))
enviar.click()
And you will need to import the following:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

Related

Problem with ActionChains - Selenium Python

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
PATH="C:\Program Files (x86)\chromedriver.exe"
driver=webdriver.Chrome(PATH)
driver.get("https://cpstest.org/")
myLinkManual= WebDriverWait(driver,1).until(
EC.presence_of_element_located((By.ID, "myLinkManual")))
clicker = WebDriverWait(driver,1).until(
EC.presence_of_element_located((By.ID, "start")))
bttnstp= WebDriverWait(driver,20).until(
EC.presence_of_element_located((By.ID, "btnstop")))
actions = ActionChains(driver)
actions.click(myLinkManual)
for i in range(20)
actions.click(clicker)
actions.perform()
I just want to navigate to the other page by clicking the locator i.e. myLinkManual
after that, on that page, I want to click on a particular area 20 times but, when I'm executing this code it's not working but when I execute only one action i.e. click(myLinkManual) or click(clicker), it works fine. Do tell me if I'm doing something wrong 'cause I'm new to this.
Thank You

How in Selenium scraping by xpath?

I would like to click the button on page: https://igs.org/network/ by xpath.
I write example code like this below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
url='https://igs.org/network/'
driver.get(url)
time.sleep(4)
myxpath = '/html/body/main/div/div/div[2]/div/div[2]/div[1]/div[2]/button[2]'
el = driver.find_element_by_xpath(myxpath).click()
and I have error like this
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/main/div/div/div2/div/div2/div1/div2/button2"}
What I'm doing wrong?
Does anyone have any ideas for navigating these elements?
The element you are trying to click is inside an iframe. You need to swith to iframe first in order to access the element.
Use WebDriverWait() and wait for frame available and switch it and following css selector.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
url='https://igs.org/network/'
driver.get(url
wait=WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src='/imaps/map.html']")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.toggler'))).click()
The xpath for that button is this one //*[#id="side-controls"]/button[2]

I have a problem with pressing a button by xpath using selenium

I want to press a button with the XPath of
//*[#id="rass-action-proceed"]
i don't know how to use selenium. can someone help me please?
This will allow you to do so using the respective XPath:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
elem = driver.find_element_by_xpath('//a[#id="rass-action-proceed"]')
elem.click() # Using WebElements 'click()' method for sheer simplicity
You can try this :
driver.find_element_by_id("rass-action-proceed").click()
or with Explicit wait :
WebDriverWait(driver , 10).until(EC.element_to_be_clickable((By.ID, "rass-action-proceed"))).click()
make sure to import :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to click the follow button on Instagram?

I am trying to click the follow button on Instagram using Python Selenium
https://www.instagram.com/luvly_zuby/?hl=en
I've tried the bellow code but it's not working.
#click follow
follow = driver.find_element_by_partial_link_text("Follo")
ActionChains(driver).move_to_element(follow).click().perform()
You can click on the element by using simple selenium click by finding the element using its text in the xpath and then using explicit wait on the element.
You can do it like:
follow_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//button[text()='Follow']")))
follow_button.click()
You need 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
This should work for you. Pass the WebElement to click(element) method.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('testUrl')
follow = driver.find_element(By.XPATH, '//button[contains(text(),'Follow')]')
webdriver.ActionChains(driver).move_to_element(follow).click(follow).perform()
Try to find the element using this css selector a.BY3EC > button and wait using .element_to_be_clickable:
follow = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.BY3EC > button')))
follow.click()
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Selenium can't find element, but element is on the https://login.aliexpress.com/ webpage

On the website the selenium script cannot find the login and password fields. I tried to search by xpath, css selector, name and class name. But nothing worked.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("https://login.aliexpress.com/")
driver.find_element_by_id("fm-login-id").send_keys("test_id")
driver.find_element_by_id("fm-login-password").clear()
driver.find_element_by_id("fm-login-password").send_keys("test_pass")
driver.find_element_by_id("fm-login-submit").click()`
I tried to do this with the help of Selenium IDE, and everything worked in the GUI. But after I exported the code to python and ran it, the program gave an error that it could not find the element.
The login form is inside of a frame, you need to switch to it first.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("https://login.aliexpress.com/")
frame = driver.find_element_by_id("alibaba-login-box")
driver.switch_to.frame(frame)
driver.find_element_by_id("fm-login-id").send_keys("test_id")
driver.find_element_by_id("fm-login-password").clear()
driver.find_element_by_id("fm-login-password").send_keys("test_pass")
driver.find_element_by_id("fm-login-submit").click()
However as the the desired elements are within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired elements to be clickable.
You can use the following solution:
Using CSS_SELECTOR:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://login.aliexpress.com/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#alibaba-login-box[src^='https://passport.aliexpress.com/mini_login.htm?']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.fm-text#fm-login-id"))).send_keys("test_id")
driver.find_element_by_css_selector("input.fm-text#fm-login-password").send_keys("test_pass")
driver.find_element_by_css_selector("input.fm-button#fm-login-submit").click()
Interim Broswer Snapshot:
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
Reference
You can find a relevant discussion in
Ways to deal with #document under iframe

Categories