How to put a text into the textfield using selenium? - python

i'm using selenium 2.46.0 and python 3.4
I want to put the string "Hello, world!" into the textfield for creating new letter.
Am i doing this the right way? I try to switch the frame and find the right element to put the text into it.
My code:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://mail.ru/')
browser.find_element_by_name('Login').clear()
browser.find_element_by_name('Login').send_keys('selenium90')
browser.find_element_by_name('Password').clear()
browser.find_element_by_name('Password').send_keys('pythonrocks')
browser.find_element_by_id('mailbox__auth__button').click()
browser.find_element_by_css_selector('a.b-toolbar__btn').click()
browser.find_element_by_css_selector('textarea.js-input:nth-child(5)').send_keys('07ufo#mail.ru')
browser.switch_to_frame(browser.find_element_by_id('compose_462_composeEditor_ifr'))
browser.find_element_by_class_name('mceContentBody').send_keys('Hello, world!')
selenium.common.exceptions.NoSuchElement
Exception: Message: Unable to locate element: {"method":"id","selector":"compose_462_composeEditor_ifr"}
I tried:
iframe = browser.find_elements_by_tag_name('iframe')[frameindex]
browser.switch_to_frame(iframe)
browser.find_element_by_css_selector('body.tinymce').send_keys('hoo')
But it did not help
i added a screenshot of tree:

The id of that element is dynamically generated! Try something like:
browser.switch_to_frame(browser.find_element_by_xpath('//frame[contains(#id, "composeEditor_ifr")]'))

Related

Want to click download csv button using Selenium Python, but button changes class-name when hovered over?

I'm trying to save a csv file by clicking the download csv button on a website. However, I noticed the .click() action not doing anything, and I found that the class-name of the button changes from 'export-button is-csv' to 'export-button is-csv hovering.' However, when I try find_element_by_class_name() on the new class name, it returns an error saying it's not there. Here's my code:
driver = webdriver.Chrome('chromedriver',options=options)
driver.get('https://www.rotowire.com/basketball/injury-report.php')
time.sleep(1)
download_csv=driver.find_element_by_class_name('export-button.is-csv')
download_csv.find_element_by_class_name('export-button.is-csv.hovering').click()
Here is the error message I receive:
Message: no such element: Unable to locate element: {"method":"css selector","selector":".export-button is-csv.hovering"}
(Session info: headless chrome=94.0.4606.71)
Was wondering what the specific fix to this is(am using Google Colabs and am new to Selenium).
Just get the table straight from the source then use pandas to convert to dataframe and write to disk:
import requests
import pandas as pd
url = 'https://www.rotowire.com/basketball/tables/injury-report.php?team=ALL&pos=ALL'
jsonData = requests.get(url).json()
df = pd.DataFrame(jsonData)
df.to_csv('file.csv', index=False)
You can directly use
button.is-csv
css selector to click on it.
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://www.rotowire.com/basketball/injury-report.php')
time.sleep(1)
download_csv = driver.find_element_by_css_selector('button.is-csv')
download_csv.click()

Type in login and password

Just began typing following code:
from selenium import webdriver
browser = webdriver.Chrome('C:\\webdrivers\\chromedriver.exe')
browser.get('https://auth.sketchengine.eu/#login')
button = browser.find_element_by_xpath('//*[#id="r_0"]')[0]
button.send_keys('Lobster')
button = browser.find_element_by_xpath('//*[#id="r_1"]')[0]
button.send_keys('123123123')
to make Python go to Sketch Engine and type in a login and password. I used inspect in Chrome and then copied the Xpaths, but Python is not doing the typing and says 'unable to locate element' for both the Xpaths. What do I change?
I am not a Python person but try this for xpath. I think the issue is that id "r_0" is on the page a few times. If you look at the xpath with "//input" you will only return one.
//input[#id="r_0"]
//input[#id="r_1"]

Selenium not identifying any element in particular HTML, why?

I have a list of URLs that I need to iterate over. The process I am working on is that selenium opens each of the URLs in the list, clicks a button to open the form, and pass some strings into the form.
I have gotten to the point of clicking the button to open the form. I can not pass in any strings into the form however using any of the elements. I get error 'Unable to locate element'
This is my code so far, easily_apply is list of the URLs:
for i in easily_apply:
driver.get(i)
test = driver.find_element_by_class_name('button')
test.click()
test.find_element_by_name("applicant.name")
test.send_keys("John Smith")
This is the HTML in question:
<input type="text" aria-describedby="label-input-applicant.name-error" aria-labelledby="label-input-applicant.name" id="input-applicant.name" name="applicant.name" class="icl-TextInput-control icl-TextInput-control--sm">
Thank you in advance.
edit:
Code with xpath, not working, getting error 'Unable to locate elements':
for i in easily_apply:
driver.get(i)
test = driver.find_element_by_class_name('indeed-apply-button')
test.click()
test.find_element_by_xpath('//input[#type="text"]')
test.send_keys("John Smith")
edit2:
code with wait in it, still getting same error 'Unable to locate elements':
for i in easily_apply:
driver.get(i)
test = driver.find_element_by_class_name('indeed-apply-button')
test.click()
wait = ui.WebDriverWait(driver,60)
test.find_element_by_name('applicant.name')
test.send_keys("John Smith")
I looked at the html, assuming you are using the code from your previous post, obtaining all the easily apply list.
The element you are looking for is inside nested iframe. you need to switch to that iframe and then look for the element
Replace the time.sleep with Webdriverwait
driver.find_element_by_css_selector('a[class="indeed-apply-button"]').click()
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[name*=indeed-ia]'))
import time
time.sleep(5)
driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
driver.find_element_by_id('input-applicant.name').send_keys('Applicant Name')
Try using xpath.
In Selenium automation, if the elements are not found by the general locators like id, class, name, etc. then XPath is used to find an element on the web page .
Syntax is:
Xpath=//tagname[#attribute='value']
Hope this helps.
driver.find_element_by_css_selector("#jl_1c27f21fec51d296 > a").click()
time.sleep(10)
driver.find_element_by_css_selector('a[class="indeed-apply-button"]').click()

Python selenium unable to find element neither by class name nor xpath

I'm newbie in Selenium. I start to learn Selenium via book. And I struggle with unclear behavior of Selenium. For educational purposes I use this site:
http://magento-demo.lexiconn.com/ - I'm trying to find search button by its class name, (which is: class='button search button') or by it xpath
search_button = self.driver.find_element_by_xpath('/html/body/div/div[2]/header/div/div[4]/form/div[1]/button')
or
search_button = self.driver.find_element_by_class_name('button')
but each time selenium unable to find it. Please help me to understand reason of such behavior. Thank you
I used Selenium IDE and it shows me XPATH: //button[#type='submit']
when I tried to find element by xpath,I have got the same error and it is strange. Please advise.
My code is:
import unittest
from selenium import webdriver
class HomePageTest(unittest.TestCase):
#classmethod
def setUpClass(cls):
#create new Firefox session
cls.driver = webdriver.Firefox()
cls.driver.implicitly_wait(30)
cls.driver.maximize_window()
#navvigate to application home page
cls.driver.get('http://magento-demo.lexiconn.com/')
def test_search__text_field_max_length(self):
#get the search text box
search_field=self.driver.find_element_by_id("search")
#check maxlenght attribute st to 128
self.assertEqual("128",search_field.get_attribute("maxlength"))
def test_search_button_enabled(self):
# get Search button
search_button = self.driver.find_element_by_class_name('button')
# check Search button is enabled
self.assertTrue(search_button.is_enabled())
#classmethod
def tearDown(self):
#close the browser window
self.driver.quit()
if __name__=='__main__':
unittest.main(verbosity=2)
Try this :
search_button = self.driver.find_element_by_xpath('//button[#class="button search-button"]')
Try downloading the selenium IDE plugin, install and start recording. Click on the button you want and view how its target is recorded in the IDE. Programmatically, selenium will accept the same xpaths and other selectors as the IDE. After it's been recorded in the IDE, there is a pull down on the target field that lets you see all the different ways you can select that element, ie xpath vs. by class etc.
http://www.seleniumhq.org/projects/ide/
you might try:
css=button.button.search-button
//button[#type='submit']
//form[#id='search_mini_form']/div/button
I think the issue is that your locator isn't specific enough. There is more than one button on the page and more than one element with class=button on the page. This CSS selector is working for me.
self.driver.find_element_by_css_selector("button[title='Search']")
Try this way using xpath locator
Explanation: Use title attribute of <button> tag.
self.driver.find_element_by_xpath("//button[#title='Search']")
OR
Explanation: Use title and type attribute of <button> tag.
self.driver.find_element_by_xpath("//button[#title='Search'][#type='submit']")

Find table elements to fill forms selenium python

My code so far is:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://moodle.tau.ac.il/')
driver.find_element_by_xpath("id('page-content')//form[#id='login']// \
input[#type='submit']").click()
Now I'm trying to fill up the login form and I succeeded to find the division
that follows id= content, easy to see in the image:
The following code line I used:
elem = driver.find_element_by_xpath("id('content'))
but it doesn't recognize anything in it and I cant get any further, what should I do to locate the input element?
It doesn't recognize anything because it is in an iframe. Therefore, you first have to switch to the iframe and then search the login form.
Switch to the iframe:
frame = driver.find_element_by_id('credentials')
driver.switch_to.frame(frame)
Or:
driver.switch_to.frame('credentials')

Categories