이희원
이희원

Reputation: 11

Auto clicking using selenium

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.

Upvotes: 1

Views: 2748

Answers (2)

Taimoor Pasha
Taimoor Pasha

Reputation: 310

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

Upvotes: 0

Peter Quan
Peter Quan

Reputation: 808

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] 한국어']"}
  1. try this kind of xpath expression
"//button[contains(@data-title,'[TXT]') and contains(@data-title,'한국어')]"
  1. scroll down to the element before clicking on it
driver.execute_script("arguments[0].scrollIntoView();", elements)
elements.click()

Upvotes: 1

Related Questions