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.
Related
i am writing a code on python by using selenium that login into Facebook and Like a Facebook page i requested. it works to login but after opening the Facebook page i requested, it wont like the page it shows error saying 'Attribute-error: 'list' object has no attribute 'click''. maybe it didn't get the correct xpath ,any ideas?
use chropath extension in chrome
See line 27 of your code: you are using find_elementS instead of find_element.
find_elements always returns a list of elements, so when you are trying to do like.click(), it fails. Try using find_element_by_xpath at the line 27 of your code, it should work.
Good luck!
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 trying to click a link by:
driver.find_element_by_css_selector("a[href='javascript:openhistory('AXS0077')']").click()
This works nice if the link opens in a new window but in this case the link actually opens a pop up window. When I try clicking the link with this method, using selenium it gives me an error:
Message: u"The given selector
a[href='javascript:openhistory('AXS0077')'] is either invalid or does
not result in a WebElement. The following error
occurred:\nInvalidSelectorError: An invalid or illegal selector was
specified"
Is this not the right way ? because
I think there may be some different way to deal with pop windows.
Your css selector could be more generic, perhaps:
driver.find_element_by_css_selector("a[href^='javascript']").click()
You've got all kinds of crazy overlapping quotation marks there. You're probably confusing it.
I have more success using find_by_xpath
Take this site as an example popups
I use firebug to inspect the element and get the xpath.
Then using the following works perfectly.
from selenium import webdriver
baseurl="http://www.globalrph.com/davescripts/popup.htm"
dr = webdriver.Firefox()
dr.get(baseurl)
dr.find_element_by_xpath("/html/body/div/center/table/tbody/tr[7]/td/div/table/tbody/tr/td[2]/div[1]/form/table/tbody/tr[4]/td[1]/a").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();
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()