Selenium/Python-如何完全扩展树



我试图完全展开一棵树,以查看所有子树,并最终能够废弃该树。但我的代码只是打开了第一层。即使只是重复任务并重新运行driver.find_elements,它也不会打开树的所有级别,我会得到一个错误:

Traceback (most recent call last):
File "/Users/hannes/Desktop/pubmed-scrapper/seleniummeshtree.py", line 18, in <module>
plus.click()
File "/Users/hannes/Desktop/pubmed-scrapper/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webelement.py", line 88, in click
self._execute(Command.CLICK_ELEMENT)
File "/Users/hannes/Desktop/pubmed-scrapper/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webelement.py", line 396, in _execute
return self._parent.execute(command, params)
File "/Users/hannes/Desktop/pubmed-scrapper/venv/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 428, in execute
self.error_handler.check_response(response)
File "/Users/hannes/Desktop/pubmed-scrapper/venv/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=106.0.5249.61)
Stacktrace:
0   chromedriver                        0x0000000104935a90 chromedriver + 3889808
1   chromedriver                        0x00000001048c4b54 chromedriver + 3427156
2   chromedriver                        0x00000001045b6108 chromedriver + 221448
3   chromedriver                        0x00000001045e83f0 chromedriver + 426992
4   chromedriver                        0x00000001045de0cc chromedriver + 385228
5   chromedriver                        0x00000001045dda10 chromedriver + 383504
6   chromedriver                        0x0000000104610944 chromedriver + 592196
7   chromedriver                        0x00000001045dc54c chromedriver + 378188
8   chromedriver                        0x00000001049092f8 chromedriver + 3707640
9   chromedriver                        0x000000010490cea8 chromedriver + 3722920
10  chromedriver                        0x00000001049133d8 chromedriver + 3748824
11  chromedriver                        0x000000010490d9a8 chromedriver + 3725736
12  chromedriver                        0x00000001048e8de8 chromedriver + 3575272
13  chromedriver                        0x0000000104927d1c chromedriver + 3833116
14  chromedriver                        0x0000000104927e84 chromedriver + 3833476
15  chromedriver                        0x000000010493c210 chromedriver + 3916304
16  libsystem_pthread.dylib             0x000000019842826c _pthread_start + 148
17  libsystem_pthread.dylib             0x000000019842308c thread_start + 8

下面你可以找到我的代码。我做错了什么?

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# load selenium safari webdriver
driver = webdriver.Chrome()
driver.get("https://meshb.nlm.nih.gov/treeView")
# click on the plus-buttons to expand the tree
pluses = driver.find_elements(By.XPATH, "//i[@onclick='openTree(this)']")
for plus in pluses:
plus.click()
time.sleep(1)
# click on the plus-buttons to expand the tree
pluses = driver.find_elements(By.XPATH, "//i[@onclick='openTree(this)']")
for plus in pluses:
plus.click()
time.sleep(1)
driver.close()

您可以使用以下方法解决此问题:
在仍然没有展开元素的情况下执行单击
您需要区分已展开和尚未展开的+元素-您的定位器不会对此进行检查
以下代码适用于我:
由于树又大又深,它将运行很长时间。

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:webdriverschromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://meshb.nlm.nih.gov/treeView'
driver.get(url)
time.sleep(2)
while driver.find_elements(By.XPATH, "//i[@onclick='openTree(this)' and(not(contains(@style,'none')))]"):
driver.find_element(By.XPATH, "//i[@onclick='openTree(this)' and(not(contains(@style,'none')))]").click()
time.sleep(0.5)

相关内容

  • 没有找到相关文章

最新更新