stuck in Webdriver python - python

Please help me out. I have tried each possibility and still no
solution.
Where the hell is webElement in python. I checked the API's and
found its in selenium.webdriver.remote.webelement
However to use this I need to create a object. webelement requires 2
arguments. What are those. any idea??
There is no send_keys for webdriver object. PLEASE help me in this.
Whenever I do driver.find_elements_by_class_name() or any other
find operation, the return should be a webelement. I cannot do any
operations on the return value. When I try to do something always get
an exception.
I tried doing webdriver.Firefox.create_webelement as well. It also
requires some argument. Any idea what argument its is?
My main issue is send_keys just doesn't work. I have reverified that I
have installed selenium in python correctly. Done everything.
Please help guys. Any suggestion would be great. If anyone shares a
tutorial then it would be great.
Thanks

First you need to find the webelement you want to interact with:
driver = webdriver.Chrome()
driver.get(url)
my_element = driver.find_element_by_id('someid')
my_element.send_keys('this is a test')
my_button = driver.find_element_by_name('button')
my_button.click()

Related

Selenium WebDriver is not selecting options from checkbox in HTML. Python

I'm developing something similar to a webscrapping to take informations from a website in html, but I'm having problems to select the options from the checkbox in the internet web page with selenium webdriver. (I'm using Python 3)
Check out part of the code:
driver.get('website_acessed')
driver.find_element(By.XPATH,'//*[#id="id_presented"]').click()
#this method is presenting this error:
---> 15 driver.find_element(By.XPATH('//*[#id="id_presented"]')).click()
TypeError: 'str' object is not callable
Can someone help me?
Thanks for the attention!
The error you're showing does not match your code.
By.XPATH is indeed a string (By.XPATH='xpath'). So it seems that you're calling driver.find_element(By.XPATH('//*[#id="id_presented"]')) in your code and By.XPATH('//*[#id="id_presented"]') is raising the error.
However, driver.find_element(By.XPATH,'//*[#id="id_presented"]') is indeed the right way to find your element.

How do I solve this error using Python to insert text into a form control / dropdown? [duplicate]

This question already has answers here:
WebDriverException: Message: TypeError: rect is undefined
(3 answers)
Closed 3 years ago.
I'm attempting to do my first web scraping project in Python using Firefox. I'm using Python version 3.7.4 and Firefox Developer 71. Both are 64 bit and I'm using Windows 10.
The problem is trying to access this form control via python. Ultimately I'd like to insert text into the form control and then web scrape the results. However, I'm just trying to click on the object at the moment since it's playing hard to get.
Here is my code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
import time
driver = webdriver.Firefox()
driver.get("https://www.website.com/login/")
jcode_form = driver.find_element_by_id('ndcCode')
hov = ActionChains(driver).move_to_element(jcode_form)
time.sleep(6)
hov.click()
hov.perform()
The error I'm receiving says:
WebDriverException: Message: TypeError: rect is undefined
Here is what I'm seeing on my browser
The object I'm trying to work with is the form control at the top.
I've tried to troubleshoot this in a number of ways using action chains and the sleep function, but I keep receiving an error message.
Any help or suggestions would be greatly appreciated.
Thank you.
There's a few different things I would try here, including send_keys(), Javascript, or a combination of both. First, I would like to modify your locator to something more unique -- the ID ndcCode may or may not be used multiple times on the page, and if we locate multiple elements then this will throw unexpected results. You can verify this as follows:
elements_count = driver.find_elements_by_id("ndcCode")
print(str(len(elements_count))
If "1" does not print, then the locator strategy will need to be updated to something more specific. Regardless, I will use an updated strategy for the following code samples.
The first approach is just a simple click() followed by send_keys():
jcode_form = driver.find_element_by_xpath("//input[contains(#placeholder, 'Start typing a Code, Drug, Product')]")
jcode_form.click()
jcode_form.send_keys("someText")
This is the most rudimentary approach, but best to rule this out first. Next, I would try Javascript approach, to perform the same actions:
jcode_form = driver.find_element_by_xpath("//input[contains(#placeholder, 'Start typing a Code, Drug, Product')]")
driver.execute_script("arguments[0].click();", jcode_form) # perform click
driver.execute_script("arguments[0].value = 'someText';", jcode_form) # set the value
I am interested to hear your results after trying either of these code samples. If they do not work for you, feel free to comment on this answer and let me know of any errors you are encountering. If possible, I would be interested to see the URL you are testing against, so that I can attempt to debug and test these code samples.
The following could should fix it:
jcode_form = driver.find_element_by_id('ndcCode')
jcode_form.click()
OR in one line:
driver.find_element_by_id('ndcCode').click()

Can't find element with Selenium even though I know the element exists

Okay, so I'm trying to find
<span class="totalcount">171</span>
on https://boston.craigslist.org/search/sss?query=food+stamps&sort=rel&search_distance=200&postal=01841
with
pagelement = driver.find_element_by_class_name('totalcount')
but for some reason I keep getting the following error
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with css selector == .totalcount
For reference, I'm using internet explorer 11 with Selenium because my boss requested I switch over to that from Firefox. Could that be what is causing the problem? (Before someone asks, I know it isn't because the page hasn't loaded yet, I added a wait specifically to deal with that.)
I did work for me, but im using find_elements as there are more than one element with the same locator strategy and also using chrome
not sure if it will help you, but you can use the below code for a better approach
Update: Tired with IE and its working
from selenium.webdriver import Ie
driver = Ie('path to IE driver')
driver.get('https://boston.craigslist.org/search/sss?
query=food+stamps&sort=rel&search_distance=200&postal=01841')
total_count = [x.text for x in driver.find_elements_by_class_name('totalcount')]
print(total_count)
['170', '170']

Selenium and Python 3 – unable to find element

First off, apologies for a commonly asked question. I've looked through all the earlier examples but none of the answers seem to work in my situation.
I'm trying to locate the username and password fields from this website: http://epaper.bt.com.bn/
I've had no problems locating the "myprofile" element and clicking on it. It then loads a page into an iframe. Here's my problem. I've tried all the various methods like find_element_by_id('input_username'), find_element_by_name('username') etc and they all do not work. Would appreciate if someone could point me down the right path.
Try first: (you should switch to iframe)
driver.switch_to.frame("iframe_login")
then you can find your elements. For example:
driver.find_element_by_id("input_username").send_keys("username")
for moving out of iframe:
driver.switch_to.default_content()

Python/Webdriver Check Select Option is Disabled

I'm using webdriver to test a specific page which sometimes will have options disabled in a form.
I'm trying to select the value directly, and then check whether or not it is enabled.
Here's what I have :
hourly = driver.find_element_by_xpath("//select[#name='frequency']/option[#value='HOURLY']")
self.assertFalse(hourly.isEnabled());
The full path is:
/html/body/div[#class='options']/form/select[#name='frequency']/option[#value='HOURLY']
When I run this snippet, I get the following :
AttributeError: 'WebElemet' object has no attribute 'isEnabled'
Which leads me to think that either:
I'm selecting the wrong thing or..
The thing I'm selecting isn't actually a WebElement as I could only find reference to isEnabled in the API under the remote driver (http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html), which wouldn't be the same thing since I'm just using Selenium Webdriver in Python.
Nevermind, I've been googling so many different docs I forgot entirely just to read the api. The call should be :
is_enabled()
rather than
isEnabled()

Categories