I am attempting to click a button on a html page using Python and selenium web driver.
This is the source code of the page http://pastebin.com/112g1Gje.
EDIT: The relevant part at is at the end.. "btn btn-primary"
I am really close to figuring this out:
The class name had spaces in it and I replaced the spaces with the dots... that made it work (Thanks to another SO post).
driver.find_element_by_css_selector(".btn.btn-primary").click()
Now the error I get is:
selenium.common.exceptions.ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
So I am trying something like:
driver.execute_script('document.querySelector("btn.btn-primary").click()')
From my understanding I have to write a javascript that clicks the button instead because the element is not visible. But that doesn't work.
The error I get is almost unreadable:
driver.execute_script('document.querySelector("btn.btn-primary").click()')
File "/Users/Richie/anaconda/lib/python3.5/site- packages/selenium/webdriver/remote/webdriver.py", line 461, in execute_script
{'script': script, 'args':converted_args})['value']
File "/Users/Richie/anaconda/lib/python3.5/site- packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "/Users/Richie/anaconda/lib/python3.5/site- packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: document.querySelector(...) is null
Anyone know?
driver.find_element_by_css_selector(".btn.btn-lg.btn-block.btn-message.open-write-message").click()
I was clicking the wrong thing.
Make sure to include the dots.
Related
I am trying to click on an element that should send me to the next page, however, I can't seem to properly locate it or even locate any item on the page.
HTML Code for Element
<a data-qa="menu-button"
class="HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG"
data-cv-test="headerSearchLink"
href="/cars"
target="_self">Search Cars
</a>
Visual of what I'm trying to click on
My code that does not work (I have the proper import statements)
PATH = "my_path_to_chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://www.carvana.com")
link = driver.find_element_by_class_name('HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')
link.click()
I have tried searching by class name using this approach and just get this error message
link = driver.find_element_by_class_name('HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".HeaderMenu__HeaderItemButton-kr6p0e-0 hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG"}
(Session info: chrome=xx.x.xxxx.xxx)
I have also tried searching by CSS selector. I even did a little sanity check and searched for the very first line of html code on the website and for some reason it gave me the same error. Anything helps... thank you!
The problem is you have spaces in the class name. The class name function doesn't work for that, I would suggest trying
driver.find_element_by_css_selector('.HeaderMenu__HeaderItemButton-kr6p0e-0.hbeYmr.MenuButton__MenuButtonWrapper-dq0g44-0.imLDTG')
driver.find_element_by_class_name('HeaderMenu__HeaderItemButton-kr6p0e-0
hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')
This code give return a node list and u can't click it , you may try get the first element , so here is the code :
driver.find_element_by_class_name('HeaderMenu__HeaderItemButton-kr6p0e-0
hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')[0]
Or bye doing this in the console of the navigator with js like this :
document.getElementByNames('HeaderMenu__HeaderItemButton-kr6p0e-0
hbeYmr MenuButton__MenuButtonWrapper-dq0g44-0 imLDTG')[0]
Edit:
I solved it by doing a find_elements_by_xpath and then using a try, catch for all elements till no error and break.
To anyone reading this question. I tried every solution here and it didn't work. The solutions were correct, the error or problem was there were multiple elements with the same xpath.
By selecting the the first element I was selecting something which was not interactable.
Getting an error when trying to click or send_keys to an input element.
st = driver.find_element_by_xpath('//input[contains(#id, "st")]')
# Element found
st.send_keys(str(amt))
# Error element not interactable
st.click()
# error element not interactable
The element:
<input id="st" type="text" value="" maxlength="7" tabindex="0">
I am able to interact and send keys to another element in the same row as this on the webpage.
Traceback:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Volumes/coding/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/Volumes/coding/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/Volumes/coding/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Volumes/coding/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=90.0.4430.72)
Any idea on how to solve or why I am running into this error?
Don't know the exact reason why the element is not interacting but you can add text to input box using JavaScript. There is a class in Selenium which allows to execute JavaScript code in browser. Do somethin like this:
script = "arguments[0].value="+str(amt)
driver.execute_script(script,st)
driver.execute_script("arguments[0].click()",st)
This will set the value of input box to the given string.
In C#:
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string script = "arguments[0].value="+amt.toString();
js.executeScript(script,st)
//to click
js.executScript("arguments[0].click()",st)
Hope it will work.
Does anybody have idea how they would automate opening and closing links on Github page of your project? So basically I wanted to automate opening and closing all the links on my Github page just to make sure none of the links are broken. Idea is it would open a link one by one (there are hundreds of links) and if the link is opened successfully it would close it and go to the next one if the link is broken it would print that link is broken and would continue to the next one.
I tried to do this in Selenium but for some reason Selenium was giving me a error that element is not interactable. I tried to find solution online but I did not have any luck. So I decided to try here maybe someone has any solution or something I would be thankful
driver.get("https://github.com/project")
links = driver.find_elements_by_xpath('//*[#href]')
for link in links:
link.click()
Here is the error:
File "d:\Python\test.py", line 15, in <module>
link.click()
File "C:\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
I didn't even get to the part where I would close the tab and print error since I am failing on first step
Image of the code and what the button looks like
Im trying to click that add button but I cant find anything that says button when i inspect element
Here is my python code im trying to get work but not sure how to do it
buttonclick = driver.find_elements_by_class_name('add-vehicle-container')
buttonclick.click()
time.sleep(1)
Errors im getting
Traceback (most recent call last):
File "C:/Users/ttttt/iCloudDrive/Documents/GitHub/SeniorProject2019TBA/Selenium/AboutYouTesting.py", line 50, in <module>
buttonclick.click()
File "C:\Users\ttttt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\ttttt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\ttttt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\ttttt\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <div class="add-vehicle-container"> could not be scrolled into view
Run one of these commands in the developer console.
getEventListeners($("#SomeElement")[0]); //Javascript
$._data($("#SomeElement")[0], "events"); //JQuery
These will tell you if the events attached are indeed "click" events, or some other type of event.
If it is indeed a click event, you can try this:
driver.execute_script("$('.add-vehicle-container').click()");
If that does not work, or the events are not click events, then you can run this:
driver.execute_script("$('.add-vehicle-container').trigger('ATTACHED_EVENT_NAME_HERE')");
You can play around with these to see if they work by simply running them in the browser developer console before trying them in your python script.
New to python and Selenium and programming for that matter.
I am trying to automate selenium to hit a specific link. In this case, I want it to hit the link associated with the link text "B":
<li>B
on this website:
http://www.lw.com
I am using this code:
def get_single_link_using_find_elements_by_link_name(url, link_name):
driver = webdriver.Firefox()
driver.get(url)
driver.implicitly_wait(10)
time.sleep(20)
element = driver.find_element_by_link_text(link_name)
element.click()
I added some wait conditions, because I thought the problem might have been a rendering problem, but they didnt help.
I am getting the following error:
Traceback (most recent call last):
File "C:\Python27\programs\selenium commands.py", line 50, in <module>
get_single_link_using_find_elements_by_link_name(url, link_name)
File "C:\Python27\programs\selenium commands.py", line 47, in get_single_link_using_find_elements_by_link_name
element = driver.find_element_by_link_text(link_name)
File "C:\Python27\lib\site-packages\selenium-2.25.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 237, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "C:\Python27\lib\site-packages\selenium-2.25.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 671, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium-2.25.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 156, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.25.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 147, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element: {"method":"link text","selector":"B"}'
Oddly enough, the same code WORKS on the following url, which is part of the same site:
http://www.lw.com/people?searchIndex=A
Any ideas?
Your code works for me, when I pass it a page that has the alphabetic listing index. This means that you're passing the wrong variables to the function-- the page that you're passing in, doesn't have a link named 'B', plain and simple.
You can check whether or not the alphabetic thing is on the page by calling driver.find_element_by_id("IndexControl1"). IndexControl1 is the name of the id in which the alphabetic thing is contained.
alphabet = driver.find_element_by_id("IndexControl1")
link_b = alphabet.find_element_by_link_text("B")
Incidentally, something else to watch out for is that if you're already on the page with "B" selected, e.g. http://www.lw.com/people?searchIndex=B&esmode=1, the letter B does not show up as a link and you will end up with a NoSuchElementException in this case, as well.
I think that covers pretty much every case where NoSuchElementException could pop up. Good luck.