I have written the following.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
bot = webdriver.Firefox()
bot.find_element_by_name("username").send_keys(config['username'])
When I am using send_keys and happen to be typing at the same instant, then what I typed is also added in the username.
How to avoid this?
Example:
I want to fill the username with "sandeep"
If at the same instant I press 'a', then the username becomes "sandeepa" or something equivalent.
You can use executeScript method:
webdriver.execute_script("document.getElementById('username').setAttribute('value', 'Sandeep')")
JavaScript will do text insertion as single operation.
I see 2 options:
Create hidden input send keys to it than perform copy/paste from hidden to visible input, after remove hidden input.
Hide input, than send_keys to it and after show it back.
Usefull links:
Performing a copy and paste with Selenium 2
WebDriver: add new element
Related
#Thanks in advance for help. New to python, tried for hour trying to correct mistake.#
Trying to locate login button element. Attached is the image of the website with the element of the login button. please see here
Below is code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
url = "https://www.xxxxxx.com/index.php/admin/index"
username = 'xxxx'
password = 'xxxxx'
driver = webdriver.Firefox(executable_path="C:\\Users\\kk\\AppData\\Local\\Programs\\Python\\Python38-32\\geckodriver.exe")
driver.get(url)
driver.find_element_by_name(name='aname').send_keys(username)
driver.find_element_by_name(name='apass').send_keys(password)
driver.find_elements_by_xpath('//style[#type="submit"]')
Rather than finding it with a CSS selector. Why not use find_element_by_xpath()
To get the XPath of that element just right-click the HTML of the input in Inspect Element, hover over Copy and you'll see "Full XPath"
Issue is your xpath.
driver.find_elements_by_xpath('//style[#type="submit"]')
Use below:
driver.find_elements_by_xpath('//input[#type="submit"]')
or
driver.find_elements_by_xpath('//input[#value="login"]')
#This is more accurate as many input tags could have type as submit
Also, please use some sort of wait as i am not sure if page will be loading fast enough every time you launch URL.
You can identify the submit button by using any of these 2:
//input[#type="submit"] or //input[#value="login"]
They should work without any problem if you don't have any similar elements on your page (which I doubt)
But if you want to be more precise, you can mix these 2 into:
//input[#value="login" and #type="submit"]
I am new in python and scraping.
I am trying to extract information about Tripadvisor. First of all, I need Selenium for crawling but when I run the program in diferents times the paths change.
I show you a example:
import urllib.request
import urllib.parse
from selenium import webdriver
import csv
from selenium.webdriver.common.action_chains import ActionChains
import time
from _datetime import datetime
from selenium.webdriver.common.keys import Keys
options=webdriver.ChromeOptions()
options.headless=False
prefs={"profile.default_content_setting_values.notofications" :2}
options.add_experimental_option("prefs",prefs)
chromedriver = "C:/Users/rober/OneDrive/Escritorio/tfm/chromedriver.exe"
driver=webdriver.Chrome(chromedriver)
driver.maximize_window()
time.sleep(5)
driver.get("https://www.tripadvisor.es/")
//*[#id="component_5"]/div/div/div/span[3]/div/div/div/a/span[2]
#Click Restaurants
driver.find_element_by_xpath('//*[#id="component_5"]/div/div/div/span[3]/div/div/div/a').click()
#Introduce localization
driver.find_element_by_xpath('//*[#id="BODY_BLOCK_JQUERY_REFLOW"]/div[14]/div/div/div[1]/div[1]/div/input').send_keys("madrid")
In the last part of code, sometimes div[14] is div[13] or div[15]. is it possible absolute xpath or use other form?
Thank you
You should not use Xpath with a longer path. That makes the test brittle
Please use shorter xpaths. An Xpath like this "//input[#class="Smftgery"]" should help you click on the same input field.
Also to click on Restaurantes, you can use //*[text()='Restaurantes']
Your Xpath is too specific, find some uniqueness in the deeper levels of the DOM. This uniqueness can be also a combination of multiple levels.
e.g. if there is only one input field inside BODY_BLOCK_JQUERY_REFLOW you can ignore all the levels in between:
'//*[#id="BODY_BLOCK_JQUERY_REFLOW"]//input'
or use some other attribute of input e.g. if it has a data attribute://input[#data="the-data-of-the-input-field"]
I am trying to use Selenium, python, and firefox to enter data into an input box on a website and select from a dropdown menu in the text box, but have been unable to do so. Formerly it was "just" a text box, but the website was changed.
The website is located at http://ecos.fws.gov/ecp/
the input textbox ID is "searchTerm". The site allows one to enter a scientific name (or part of a scientific name) and then select from options. For example, if you enter "Acipenser brevirostrum" into the textbox, it will provide you with a single option to click on.
Unfortunately, not sure how to code this up. Any help would be appreciated. So far I have this.
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.support.ui import Select
binary = FirefoxBinary(r'C:/Program Files (x86)/Mozilla Firefox/firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)
driver.get("http://ecos.fws.gov/ecp")
SciName = driver.find_element_by_id('searchTerm')
SciName.send_keys(names)
SciName.send_keys(Keys.RETURN)
The last three lines used to work, but now that it is both an input box with a drop down menu, it fails.
Any help would be appreciated.
After you enter the name in the search box (id -- searchTerm), check for the visibility of the div (class -- autocomplete-suggestions) with an explicit wait. This contains the suggestions that the site throws up. Then you have to find the option which works for you. Try this xpath and click on the element. You should use findelements option as your text might not match anything and you will get an error.
"//div[#class='autocomplete-suggestion'][contains(.,'Acipenser brevirostrum')]"
If you have problems getting the autocomplete dropdown when you type the search text, send the text one character at a time with a small interval in between to trigger the call.
you can simply use driver.find_element_by_name,as html is:
<input type="text" class="form-control" name="query" placeholder="Search ECOS">
This code will work:
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://ecos.fws.gov/ecp/")
driver.find_element_by_name("query").click()
driver.find_element_by_name("query").send_keys("test")
driver.find_element_by_css_selector("button.btn.btn-default").click()
I have a basic Selenium script that makes use of the chromedriver binary. I'm trying to display a page with recaptcha on it and then hang until the answer has been completed and then store that in a variable for future use.
The roadblock I'm hitting is that I am unable to find the recaptcha element.
#!/bin/env python2.7
import os
from selenium import webdriver
driverBin=os.path.expanduser("~/Desktop/chromedriver")
driver=webdriver.Chrome(driverBin)
driver.implicitly_wait(5)
driver.get('http://patrickhlauke.github.io/recaptcha/')
Is there anything special needed to be able to see this element?
Also is there a way to grab the token after user solve without refreshing the page?
As it is now the input type of the recaptcha-token id is hidden. After solve a second recaptcha-token id is created. This is the value I wish to store in a variable. I was thinking of having a loop of checking length of found elements with that id. If greater than 1 parse. But I'm unsure whether the source updates per se.
UPDATE:
With more research it has to do with the nature of the element, particularly: with the tag: <input type="hidden". So I guess to rephrase my question, how does one extract the value of a hidden element.
The element you are looking for (the input) is in an iframe. You'll need switch to the iframe before you can locate the element and interact with it.
import os
from selenium import webdriver
driver=webdriver.Chrome()
try:
driver.implicitly_wait(5)
driver.get('http://patrickhlauke.github.io/recaptcha/')
# Find the iframe and switch to it
iframe_path = '//iframe[#title="recaptcha widget"]'
iframe = driver.find_element_by_xpath(iframe_path)
driver.switch_to.frame(iframe)
# Find the input element
input_elem = driver.find_element_by_id("recaptcha-token")
print("Found the input element: ", input_elem)
finally:
driver.quit()
I am trying to type a float number into a textbox with default value 0.00.But it tries to get appended instead of overwriting it.I tried with .clear() and then send_keys('123.00') but still it gets appended.
Then i tried with send_keys(Keys.CONTROL+'a','123.00').It updates 0.00 only.
Any help is really appreciated.
For more info ..
URL : http://new.ossmoketest.appspot.com
userid: senthil.arumugam#mycompanyname.com -- mycompanyname = orangescape (sorry to avoid spam mails)
password not needed now.
click purchaseorder... in the form please new product and new price... sample application for automation.. thanks
I've had good results with:
from selenium.webdriver.common.keys import Keys
element.send_keys(Keys.CONTROL, 'a')
element.send_keys('123.00')
If that doesn't work it may have something to do with the code in the web page.
Unless you have custom editbox, click() should work for you:
from selenium.webdriver import Firefox
b = Firefox()
b.get('http://google.com')
e = b.find_element_by_id('lst-ib')
e.click() # is optional, but makes sure the focus is on editbox.
e.send_keys('12.34')
e.get_attribute('value')
# outputs: u'12.34'
e.click()
e.clear()
e.get_attribute('value')
# outputs: u''
e.send_keys('56.78')
e.get_attribute('value')
# outputs: u'56.78'
I just found the clear() command - see here:
If this element is a text entry element, this will clear the value. Has no effect on other elements. Text entry elements are INPUT and TEXTAREA elements.
EDIT:
So your approach would be:
element.clear();
element.sendKeys('123.00');
I've experienced issues with all the examples given in other answers.
el.send_keys(Keys.CONTROL + 'a' + Keys.NULL, 'your string')
Has worked in all the projects I've worked in, so much I've wrapped it into my own implementation of the Webdriver class with more robust operations.