Auto clicking using selenium - python

By using selenium click, I'm looking to auto download the subtitles that has 'TXT Korean' in their button tags in the webpage source. I am trying to collect Korean subtitles data for data collection.
the code below opens the website fine, but wouldn't click the 'TXT 한국어' part and giving me errors.
The sourcecode from the website
<button type="button" class="ma-1 download-button v-btn v-btn--depressed v-btn--flat v-btn--outlined theme--light v-size--default primary--text" data-title="[TXT] 한국어"><span class="v-btn__content"><i aria-hidden="true" class="v-icon notranslate v-icon--left material-icons theme--light">save_alt</i><button type="button" class="primary v-btn v-btn--depressed v-btn--rounded theme--light v-size--x-small"><span class="v-btn__content">TXT</span></button></span></button>
Python Code
from selenium import webdriver
driver = webdriver.Chrome(r'C:/Users/ccc/Desktop/chromedriver.exe')
driver.get('https://downsub.com/?url=https%3A%2F%2Fwww.ted.com%2Ftalks%2Frachel_kleinfeld_a_path_to_security_for_the_world_s_deadliest_countries%3Flanguage%3Dko%27language%3D%27ko%2r')
elements = driver.find_element_by_xpath("//div[#class='layout justify-start']//button[#data-title='[TXT] 한국어']")
elements.click()
ERROR
Traceback (most recent call last):
File ".\down.py", line 5, in <module>
elements = driver.find_element_by_xpath("//div[#class='layout justify-start']//button[#data-title='[TXT] 한국어']")
File "C:\Users\ccc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\ccc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\ccc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\ccc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\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":"xpath","selector":"//div[#class='layout justify-start']//button[#data-title='[TXT] 한국어']"}
(Session info: chrome=85.0.4183.121)
PS C:\Users\ccc\Desktop> [18904:19076:0928/165026.096:ERROR:ssl_client_socket_impl.cc(963)] handshake failed; returned -1, SSL error code 1, net_error -113
[18904:19076:0928/165026.332:ERROR:ssl_client_socket_impl.cc(963)] handshake failed; returned -1, SSL error code 1, net_error -113
[7460:6468:0928/165026.860:ERROR:device_event_log_impl.cc(208)] [16:50:26.860] Bluetooth: bluetooth_adapter_winrt.cc:1074 Getting Default Adapter failed.

I rewrote your code in Java, the code worked fine, no error happened.
So, just according to your below error message, there are some suggests you could try
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='layout justify-start']//button[#data-title='[TXT] 한국어']"}
try this kind of xpath expression
"//button[contains(#data-title,'[TXT]') and contains(#data-title,'한국어')]"
scroll down to the element before clicking on it
driver.execute_script("arguments[0].scrollIntoView();", elements)
elements.click()

Like #Peter answered, let me give you little more insight, Your desired element is not displaying in current view, That's why Selenium is throwing you this exception.
You need to scroll down to that element and then click it.
It will do the trick.
elements = driver.find_element_by_xpath("//div[#class='layout justify-start']//button[#data-title='[TXT] 한국어']") //Element declaration
driver.execute_script("arguments[0].scrollIntoView();", elements) //Scroll down to that element
elements.click() //Click on element

Related

Python Selenium - No Such Element Found... I'm Lost

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]

Selenium Testing unable to target element

I am running some testing on our website using selenium.
At the login page I would like to target the login button and click it.
the source code of the page looks like this:
I am trying to target the second button that has the class=OTSigninButton by using its xpath.
so here is the python code.
time.sleep(5)
element = driver.find_element_by_xpath('//*[#id="root"]/div/div[1]/div/svg[2]/path')
element.click()
but when I run the code I get the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="root"]/div/div[1]/div/svg[2]/path"}
(Session info: chrome=91.0.4472.106)
Here is the weird part. The login page has 2 buttons, their xpath is similar as its a list.
the first button is:
//*[#id="root"]/div/div[1]/div/svg[1]/path
and the second button(the one I want to target) is
//*[#id="root"]/div/div[1]/div/svg[1]/path
As both of them has the id=root, if I find_element_by_id and target root, I am able to click on the first button.
Here is where I am struggling a lot, how can I use the xpath to target the second button?
thank you so much for your time and help guys.
EDIT: Full error
Traceback (most recent call last):
File "test.py", line 17, in <module>
element = driver.find_element_by_xpath("//*[local-name()='svg' and contains(#class, 'OTSigninButton')]/title")
File "/Users/<user>/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/Users/<user>/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Users/<user>/opt/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/<user>/opt/anaconda3/lib/python3.8/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":"xpath","selector":"//*[local-name()='svg' and contains(#class, 'OTSigninButton')]/title"}
(Session info: chrome=91.0.4472.106)
class=OTSigninButton
is a attribute of SVG. These are special tags, you can not just write their tag name in console and locate them.
try this instead :
//*[local-name()='svg' and contains(#class, 'OTSigninButton')]

Element Not Interactable

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.

How to edit chromes search and homepage with selenium/python?

I am trying to edit chrome browsers search and homepage using selenium/python. After navigating to chrome://settings/searchEngines and targeting the 'add' button with the ID 'addSearchEngine', I get an error when I run a .click function. How do I target this element correctly, or is there another way to update chromes search/startpage with python?
I'm guessing this element is trapped inside an iframe but I'm unable to find one on the page using the dev tools, xpath noted the following about the absolute xpath: "It might be a child of iframe from different src & it is not supported currently."
from selenium import webdriver
driver = webdriver.Chrome()
driver.set_page_load_timeout(10)
driver.get("chrome://settings/searchEngines")
driver.find_element_by_id("addSearchEngine").click()
Traceback (most recent call last):
File "C:/Users/Jonathan/PycharmProjects/test_project/test_project/Main.py", line 20, in <module>
driver.find_element_by_id("addSearchEngine").click()
File "C:\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python37-32\lib\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":"[id="addSearchEngine"]"}
(Session info: chrome=75.0.3770.142)
chrome://settings/searchEngines has Shadow DOM elements . You will need to use the driver.execute_script() to get handle of the shadowRoot element and ultimately get to the 'addSearchEngine' element.
Example python : shadowRoot python
Example Java : For chrome://downloads/ shadowRoot java
Refer to this post for detailed explanation.
In your case you can do the below.
url = "chrome://settings/searchEngines"
driver.get(url)
addButton = driver.execute_script("return document.querySelector('settings-ui')"
".shadowRoot.querySelector('#main')"
".shadowRoot.querySelector('settings-basic-page.showing-subpage')"
".shadowRoot.querySelector('settings-search-page')"
".shadowRoot.querySelector('settings-search-engines-page')"
".shadowRoot.querySelector('#addSearchEngine')")
addButton.click()
Screenshot:

Python Selenium cannot find an element on a page by xpath

I am using python and selenium to test some things with fantasy football. Here is my code so far (I just started).
import time
from selenium import webdriver
driver = webdriver.Chrome('C:\\Users\\202300Fontenot\\Desktop\\3\\chromedriver.exe')
driver.get('http://games.espn.com/ffl/signin?redir=http%3A%2F%2Fgames.espn.com%2Fffl%2Fclubhouse%3FseasonId%3D2018%26leagueId%3D49607%26teamId%3D4');
driver.implicitly_wait(10)
time.sleep(10)
search_box = driver.find_element_by_xpath('//*[#id="did-ui-view"]/div/section/section/form/section/div[1]/div/label/span[2]/input')
search_box.send_keys('email#icloud.com')
search_box.submit()
time.sleep(5)
driver.quit()
This just tries to enter an email address into the box. I am getting this error every time:
Traceback (most recent call last):
File "C:\Users\202300Fontenot\Desktop\3\ESPN.py", line 8, in <module>
search_box = driver.find_element_by_xpath('//*[#id="did-ui-view"]/div/section/section/form/section/div[1]/div/label/span[2]/input')
File "C:\Users\202300Fontenot\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 393, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\202300Fontenot\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
'value': value})['value']
File "C:\Users\202300Fontenot\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\Users\202300Fontenot\AppData\Local\Programs\Python\Python36\lib\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":"xpath","selector":"//*[#id="did-ui-view"]/div/section/section/form/section/div[1]/div/label/span[2]/input"}
(Session info: chrome=69.0.3497.92)
(Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 10.0.16299 x86_64)
Thanks for your help.
Your problem is that the input you are trying to select exists inside of an iframe. You must first tell the driver to switch to the iframe and then execute the xpath selection.
driver.switch_to.frame("disneyid-iframe")
driver.find_element_by_xpath("//input[#type='email']").send_keys('email#icloud.com')
First of all you need to switch to your iframe using its name:
disneyid-iframe
Also if XPATH is not the only choice, you may use the CSS Selector:
div.field-username-email input[type='email']
This will get the driver to find & fill your field.
Using XPath in python You need to provide the string for
driver.find_element_by_xpath
Example Code below
("//*[#id=\"did-ui-view\"]/div/section/section/form/section/div[1]/div/label/span[2]/input")
Please refer below document regarding String
String Gude first Para

Categories

Resources