Problem with ActionChains - Selenium Python - 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

Related

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]

Why selenium is not clicking on the submenu in the drop down

Here is the code I'm trying to use to scrape data from FRED website to download the time series data in CSV format but it is redirecting me top another page
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
url='https://fred.stlouisfed.org/series/TERMCBAUTO48NS'
driver=webdriver.Chrome(executable_path=r'D:\\Workspace\\Python\\automation\\chromedriver.exe')
driver.get(url)
element=driver.find_element_by_id('download-button')
element.click()
wait1=WebDriverWait(driver,20)
result1=wait1.until(
EC.element_to_be_clickable((By.ID,'fg-download-menu')))
print('Result 1: ',result1)
menu=driver.find_element_by_id('fg-download-menu')
wait1=WebDriverWait(driver,20)
result2=wait1.until(
EC.element_to_be_clickable((By.ID,'download-data-csv')))
print('Result 2: ',result2)
hidden_submenu=driver.find_element_by_id('download-data-csv')
actions=ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
driver.quit()
The locators you are using are not unique. Like there are several tags with the same id download-button.
Its important to find unique locators for Elements. You can refer This link
Try like below and confirm:
driver.get("https://fred.stlouisfed.org/series/TERMCBAUTO48NS")
wait = WebDriverWait(driver,30)
# Click on Download button
wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#id='page-title']//button[#id='download-button']"))).click()
# Click on CSV data
wait.until(EC.element_to_be_clickable((By.XPATH,"//ul[#id='fg-download-menu']//a[#id='download-data-csv']"))).click()
This should work:
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.Chrome("D:/chromedriver/94/chromedriver.exe")
driver.get("https://fred.stlouisfed.org/series/TERMCBAUTO48NS")
# wait 60 seconds
wait = WebDriverWait(driver,60)
wait.until(EC.element_to_be_clickable((By.ID, "download-button"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "download-data-csv"))).click()

Dropdown menu not clicking in python selenium

import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.remote import webelement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time
url = "https://www.mrporter.com/en-gb/mens/product/nike/shoes/low-top-sneakers/space-hippie-04-recycled-stretch-knit-sneakers/19971654707345242"
PATH = 'C:\Program Files (x86)\chromedriver.exe'
browser = webdriver.Chrome(PATH)
browser.get(url)
element_dropdown = browser.find_element_by_class_name("CombinedSelect11__field CombinedSelect11__field--selectableOption CombinedSelect11__field--nativeSelect")
select = Select(element_dropdown)
try:
select.select_by_visible_text("8")
except NoSuchElementException:
print("the item doesnt exist")
I am trying to locate the dropdown menu of the link in my code. Once the dropdown box is located I want to search by visible text for a size 8. However whatever I try it still doesn't work.
You can try using explicit wait and then perform your operation. Please take a look at the below code which I have written to replicate your scenario. It's working fine for me. Do let me know if you face any problems.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options)
driver.get(
'https://www.mrporter.com/en-gb/mens/product/nike/shoes/low-top-sneakers/space-hippie-04-recycled-stretch-knit-'
'sneakers/19971654707345242')
wait = WebDriverWait(driver, 30)
wait.until(EC.visibility_of_element_located((By.XPATH, '//div[text()="Select a size"]'))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, '//li[#data-value="8"]'))).click()

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

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

Python Selenium not able to find element for Create New Account button on website

Here is my code so far, note that the web page loads up with a captcha. I countered this with adding a time.sleep so I can run tests. When I try to submit the form by submitting "Create new account", I get an error saying the element has no attribute for 'submit'. I tried finding the element using xpath, css_selectos, tags, class names, etc.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
browser = webdriver.Chrome()
browser.get('https://www.bstn.com/en/register/address')
time.sleep(35)
elam = browser.find_element_by_css_selector("[value='Create new account']")
elam.Submit()
If you are trying to click on Create new account button after filling information then please find below xpath to click on it
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//input[#class='button radius charcheck-submit']"))).click()
Another solution with action class
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import ActionChains
actionChains = ActionChains(driver)
submit = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, "//input[#class='button radius charcheck-submit']")))
actionChains.move_to_element(submit).click().perform()
Working code
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
# browser = webdriver.Chrome()
browser.get('https://www.bstn.com/en/register/address')
time.sleep(35)
WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.XPATH, "//input[#class='button radius charcheck-submit']"))).click()

Categories