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()
Related
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.
Keep bumping into the same problem: when I use Selenium (Python), I often need to find the element to send_keys() to. E.g. on ebay front webpage, need to search for some item.
Each time I end up tring all the classes/frames around what I need (sometimes with ChroPath) and one of them works. Is there a simpler and more systematic way to find the element corresponding to search tab to send keys to?
Well one thing you could try is using hasattr method to check if the element has a particular attribute or not. From the official documentation about hasattr
hasattr(object, name) The arguments are an object and a string. The
result is True if the string is the name of one of the object’s
attributes, False if not. (This is implemented by calling
getattr(object, name) and seeing whether it raises an AttributeError
or not.)
if hasattr(element, 'send_keys'):
# do something
No, you shouldn't try to invoke send_keys() on random elements on a webpage, e.g. ebay homepage.
Ideally, the WebElements with whom you would interact or invoke send_keys() should be part of your tests and should also be a part of the Test Plan.
While you execute your tests, in the due coarse you may encounter different errors when send_keys() is being invoked. You have a to address the issues as they show up as per the prevailing condition of the HTML DOM.
References
You can find a couple of relevant discussions on send_keys() in:
How to insert a value in a field Text using Selenium?
Using Selenium in Python to enter currency format text
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()
I am very much new to selenium WebDriver and I am trying to automate a page which has a button named "Delete Log File". Using FireBug I got to know that, the HTML is described as
and also the css selector is defined as "#DeleteLogButton" using firepath
hence I used
browser.find_element_by_css_selector("#DeleteLogButton").click() in webdriver to click on that button but its now working and also, I tried,
browser.find_element_by_id("DeleteLogButton").click() to click on that button. Even this did not find the solution for my problem...
Please help me out in resolving the issue.
Most of the times im using By.xpath and it works specially if you use contains in your xpath. For example : //*[contains(text(),'ABC')]
This will look for all the elements that contains string 'ABC'
In your case you can replace ABC with Delete Log File
try to find it by name like :
browser.find_element_by_name("Delete Log File").click();
I'm not sure how to find this information, I have found a few tutorials so far about using Python with selenium but none have so much as touched on this.. I am able to run some basic test scripts through python that automate selenium but it just shows the browser window for a few seconds and then closes it.. I need to get the browser output into a string / variable (ideally) or at least save it to a file so that python can do other things on it (parse it, etc).. I would appreciate if anyone can point me towards resources on how to do this. Thanks
using Selenium Webdriver and Python, you would simply access the .page_source property to get the source of the current page.
for example, using Firefox() driver:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.example.com/')
print(driver.page_source)
driver.quit()
There's a Selenium.getHtmlSource() method in Java, most likely it is also available in Python. It returns the source of the current page as string, so you can do whatever you want with it
Ok, so here is how I ended up doing this, for anyone who needs this in the future..
You have to use firefox for this to work.
1) create a new firefox profile (not necessary but ideal so as to separate this from normal firefox usage), there is plenty of info on how to do this on google, it depends on your OS how you do this
2) get the firefox plugin: https://addons.mozilla.org/en-US/firefox/addon/2704/ (this automatically saves all pages for a given domain name), you need to configure this to save whichever domains you intend on auto-saving.
3) then just start the selenium server to use the profile you created (below is an example for linux)
cd /root/Downloads/selenium-remote-control-1.0.3/selenium-server-1.0.3
java -jar selenium-server.jar -firefoxProfileTemplate /path_to_your_firefox_profile/
Thats it, it will now save all the pages for a given domain name whenever selenium visits them, selenium does create a bunch of garbage pages too so you could just delete these via a simple regex parsing and its up to you, from there how to manipulate the saved pages