I'm running into an issue with my Selenium script on Python. In the javascript web application that I'm interacting with, an element I need to click doesn't exist until I hover over it. I've looked and found various answers on how to hover, but the sequence needs to include the clicking of a new element during the hover event. Here is the code I am currently working with. The element is renamed from add to add1 when a hover occurs, once add1 exists; I should be able to click/send.keys to execute said element.
...
driver = webdriver.Firefox()
from selenium.webdriver.common.action_chains import ActionChains
...
add = driver.find_element_by_css_selector('input.add')
Hover = ActionChains(driver).move_to_element(add)
Hover.perform()
SearchButton = driver.find_element_by_css_selector('input.add1')
SearchButton.click()
I'm new with Python and with programming in general, but I can't figure out how to sequence this correctly.
Any help would be greatly appreciated.
Following had worked for me, please give a try:
add = driver.find_element_by_css_selector('input.add')
SearchButton = driver.find_element_by_css_selector('input.add1')
Hover = ActionChains(driver).move_to_element(add).move_to_element(SearchButton)
Hover.click().build().perform()
I'm not sure about above Python code. But you can use above logic.
here another useful link
How to mouseover in python Webdriver
#TDHM
you should mention this below line to make it works
from selenium.webdriver.common.action_chains import ActionChains
thank you
Related
I have a python script that uses Selenium and, in the script, a Google Login (OAuth) button is clicked and a new window is displayed as shown below:
In the new window, thanks to the superb UX :P, the input element for the email field is already selected. Since the field is already selected, can I use Selenium to just start typing without finding the input element and using send_key?
Just switch to active element .switch_to.active_element :
element = driver.switch_to.active_element
element.send_keys('value')
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.send_keys('hotmail.com')
actions.perform()
Try using action chains to send the keys over.
I'm using Selenium in Python to click a text entry field and write some text into it. Neither the .click() nor .send_keys() methods are being recognized. Can someone help with this?
Also, is there a way to stop Selenium from printing to the console automatically? My program is console-based and Selenium is writing things to an input() that I gave because it prints to the console.
Here is a code snippet:
url = "https://weather.com/weather/today/l/69ef4b6e85ca2de422bea7adf090b06c1516c53e3c4302a01b00ba763d49be65"
browser = webdriver.Edge("Web Scrapers\msedgedriver.exe")
browser.get(url)
textbox = browser.find_element_by_id("LocationSearch_input")
textbox.click()
textbox.send_keys(zipcode)
textbox.send_keys(Keys.ENTER)
you could try the explicitWait hope this will work for you
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//input[#type='text']"))).send_keys("20874",Keys.RETURN)
I would suggest you to do it with explicit wait.
I am giving this answer, cause none of the answer's are really using Explicit waits with ID attribute :
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://weather.com/weather/today/l/69ef4b6e85ca2de422bea7adf090b06c1516c53e3c4302a01b00ba763d49be65")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "LocationSearch_input"))).send_keys('60007' + Keys.RETURN)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class^='CurrentConditions--dataWrapperInner']"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
From what I can tell, everything is working fine here, however you are pointing your textbox variable to the wrong HTML element.
LocationSearch_input is a hidden label that isn't directly attached to the searchbox. I would try pointing it to
SearchInput--SearchInput--M7lKl SearchInput--enableSearchIcon--1Ugsx, one of the parent elements.
At least testing in firefox, it's a timing thing. The click works. The error of element not interactable (not reachable by keyboard) comes off of the send_keys line. If we wait after the click(), the element becomes reachable by keyboard.
The following could probably be refined (really don't like sleep, but sometimes it works), but works for me:
url = "https://weather.com/weather/today/l/69ef4b6e85ca2de422bea7adf090b06c1516c53e3c4302a01b00ba763d49be65"
browser.get(url)
browser.find_element_by_id('LocationSearch_input').click()
time.sleep(5)
browser.find_element_by_id('LocationSearch_input').send_keys('Boston')
At that point you need to click on whichever of the 10 options is what you really want.
I am trying to automate a task which can save me thousands of clicks.
I searched a bit for available modules in Python and I have selected to work with Selenium.
I installed Firefox drivers and did some progress but I am stuck for a long time. I finally gave up, opened a Stack Overflow account and wanted to bring this problem into this helpful medium.
My code can successfully do some clicks, but I could not make the code click a button like element. I have to click such items so that page opens some new elements. (Hopefully I am going to click on these new elements to save some excels files automatically).
Here is the part which works as expected:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
browser = webdriver.Firefox()
url = "https://www.tbb.org.tr/tr/bankacilik/banka-ve-sektor-bilgileri/istatistiki-raporlar/59"
browser.get(url)
time.sleep(2)
bank_reports_element_xpath = "//*[#title= 'Tüm Raporlar']"
bank_reports_element = browser.find_element_by_xpath(bank_reports_element_xpath)
bank_reports_element.click()
time.sleep(2)
second_item = "//div[#data-catid = '52']"
finance_tables_element = browser.find_element_by_xpath(second_item)
finance_tables_element.click()
years_item = "//div[#class = 'years']"
years_element = finance_tables_element.find_element_by_xpath(years_item)
year_elements = years_element.find_elements_by_xpath("a")
There I would like to click on the years.
a screenshot of the years that I can't click using Selenium
As an example, I get the element related to year 2004.
year2004 = year_elements[2]
Issuing a year2004.click() command gives an ElementNotInteractableException exception.
year2004.click() # ElementNotInteractableException: Element could not be scrolled into view
Based on searching similar posts, I tried the following (executing the click via javascript). I got no error but it does not do anything. When I click the year2004 button with mouse, a list pops-up in the page. But when I run the below code, no list pops up in the page.
browser.implicitly_wait(4)
browser.execute_script("arguments[0].scrollIntoView();", year2004)
browser.execute_script("arguments[0].click()", year2004)
I tried also the following code. I get "TypeError: rect is undefined"
browser.implicitly_wait(4)
action = ActionChains(browser)
action.move_to_element(year2004) # gives: TypeError: rect is undefined
action.click(on_element = year2004) # gives: TypeError: rect is undefined
action.perform()
I tried using Chrome Driver and got similar results. The errors definitions were not exactly the same. Instead of the above "rect is undefined" error, Chrome error is like: "Javascript error: Failed to execute 'elementsFromPoints' on 'Document': Provided double value is non-finite"
I don't know if it is relevant but year2004.location dictionary has a 0 value for "x".
I would appreciate any answer,
Keep safe
Use this xpath will give you all the elements of year in 52 category
//div[#data-catid='52']/descendant-or-self::*/div[#class='years']/child::a
for scrolling try
browser.send_keys(Keys.PAGE_DOWN)
I checked manually and page down button is working for me press it twice for desire element
I have a page whose source code is not available, but there is a input box where cursor is blinking.
Can i write something into the text box without finding the element. I mean, some way where send key can automatically look for focused inputbox and type input to it.
My code does not work obviously
driver.send_keys("testdata")
Solved it
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(self.driver)
actions.send_keys('dummydata')
actions.perform()
If you get error about 'self' in this code:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(self.driver)
actions.send_keys('dummydata')
actions.perform()
just use:
actions = ActionChains(driver)
I don't have comment rights that's why I put this as answer
Edit: Added this enhancement as a comment on the original answer.
This worked for me:
driver.find_element_by_tag_name('body').send_keys(' ')
(Which I used to use a space character to scroll through a page)
Using the Python version of Selenium, is it possible to click some element in the DOM and to specify the coordinates where you want to click it?
The Java version has the method clickAt, which actually does exactly what I am looking for, but can't find the equivalent in Python.
This should do it! Namely you need to use action chains from webdriver. Once you have an instance of that, you simply register a bunch of actions and then call perform() to perform them.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.google.com")
el=driver.find_elements_by_xpath("//button[contains(string(), 'Lucky')]")[0]
action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element_with_offset(el, 5, 5)
action.click()
action.perform()
This will move the mouse 5 pixels down and 5 pixels right from the upper-left corner of the button I feel lucky. Then it will click().
Notice that you must use perform(). Else nothing will happen.
The reason you are getting confused is clickAt is an old v1 (Selenium RC) method.
WebDriver has a slightly different concept, of 'Actions'.
Specifically, the 'Actions' builder for the Python bindings live here.
The idea of the clickAt command is to click at a certain position relative to a particular element.
The same is achievable within the WebDriver, using the 'Actions' builder.
Hopefully this updated documentation can help.
You can perform the task with the Action chains in the python specially with Edge browser:
from selenium.webdriver import ActionChains
actionChains = ActionChains(driver)
button_xpath = '//xapth...'
button = driver.find_element_by_xpath(button_xpath)
actionChains.move_to_element(button).click().perform()
But sometimes Action chain does not finds the DOM element. Hence better option to use execute scipt in following way:
button_xpath = '//xapth...'
button = driver.find_element_by_xpath(button_xpath)
driver.execute_script("arguments[0].click();", button)
I've not personally used this method, but looking through the source code of selenium.py I've found the following methods that look like they'd do what you want - They look to wrap clickAt:
def click_at(self,locator,coordString):
"""
Clicks on a link, button, checkbox or radio button. If the click action
causes a new page to load (like a link usually does), call
waitForPageToLoad.
'locator' is an element locator
'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse event relative to the element returned by the locator.
"""
self.do_command("clickAt", [locator,coordString,])
def double_click_at(self,locator,coordString):
"""
Doubleclicks on a link, button, checkbox or radio button. If the action
causes a new page to load (like a link usually does), call
waitForPageToLoad.
'locator' is an element locator
'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse event relative to the element returned by the locator.
"""
self.do_command("doubleClickAt", [locator,coordString,])
They appear in the selenium object and here is their online API documentation.