使用selenium自动单击

2024-04-19 11:24:11 发布

您现在位置:Python中文网/ 问答频道 /正文

通过使用selenium click,我希望能够自动下载网页源中按钮标签中带有“TXT韩语”的字幕。我正在尝试收集韩语字幕数据以进行数据收集

下面的代码可以很好地打开网站,但不会单击'TXT한국어'分手并给我错误

网站的源代码

<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代码

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()

错误

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.

Tags: pytxtpackagesseleniumlinebuttonelementfind
2条回答

就像@Peter Answeed,让我再给你一点洞察力,你想要的元素没有显示在当前视图中,这就是为什么Selenium抛出这个异常。 您需要向下滚动到该元素,然后单击它。 它会成功的

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

我用Java重写了您的代码,代码运行良好,没有发生错误

因此,根据下面的错误消息,有一些建议您可以尝试

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. 试试这种xpath表达式
"//button[contains(@data-title,'[TXT]') and contains(@data-title,'한국어')]"
  1. 在单击元素之前向下滚动到该元素
driver.execute_script("arguments[0].scrollIntoView();", elements)
elements.click()

相关问题 更多 >