如何在 jupyterhub 页面中使用 python-selenium 查找现有的 HTML 元素?

Ale*_*lex 6 python selenium python-3.x

我在 HTML 页面中有以下构造,我想选择li元素(使用 python-selenium):

<li class="p-Menu-item p-mod-disabled" data-type="command" data-command="notebook:run-all-below">
    <div class="p-Menu-itemIcon"></div>
    <div class="p-Menu-itemLabel" style="">Run Selected Cell and All Below</div>
    <div class="p-Menu-itemShortcut" style=""></div>
    <div class="p-Menu-itemSubmenuIcon"></div>
</li>
Run Code Online (Sandbox Code Playgroud)

我正在使用以下 xpath:

//li[@data-command='notebook:run-all-below']
Run Code Online (Sandbox Code Playgroud)

但是似乎没有找到该元素。

完整的、最少的工作示例代码:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("https://mybinder.org/v2/gh/jupyterlab/jupyterlab-demo/master?urlpath=lab/tree/demo")

# Wait for the page to be loaded
xpath = "//button[@title='Save the notebook contents and create checkpoint']"
element = WebDriverWait(driver, 600).until(
    EC.presence_of_element_located((By.XPATH, xpath))
)
time.sleep(10)
print("Page loaded")

# Find and click on menu "Run"
xpath_run = "//div[text()='Run']"
element = WebDriverWait(driver, 60).until(
    EC.element_to_be_clickable((By.XPATH, xpath_run))
)
element.click()
print("Clicked on 'Run'")

# Find and click on menu entry "Run Selected Cell and All Below"
xpath_runall = "//li[@data-command='notebook:run-all-below']"
element = WebDriverWait(driver, 600).until(
    EC.element_to_be_clickable((By.XPATH, xpath_runall))
)
print("Found element 'Run Selected Cell and All Below'")
element.click()
print("Clicked on 'Run Selected Cell and All Below'")

driver.close()
Run Code Online (Sandbox Code Playgroud)

环境:

  • MacOS 莫哈韦沙漠 (10.14.6)
  • 蟒蛇 3.8.6
  • 硒 3.8.0
  • 壁虎驱动程序 0.26.0

附录

我一直在尝试使用 Firefox “Selenium IDE”附加组件记录这些步骤,它为 python 提供以下步骤:

sdriver.get("https://hub.gke2.mybinder.org/user/jupyterlab-jupyterlab-demo-y0bp97e4/lab/tree/demo")
driver.set_window_size(1650, 916)
driver.execute_script("window.scrollTo(0,0)")
driver.find_element(By.CSS_SELECTOR, ".lm-mod-active > .lm-MenuBar-itemLabel").click()
Run Code Online (Sandbox Code Playgroud)

当然,这也不起作用。使用这些代码行,我收到一个错误

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .lm-mod-active > .lm-MenuBar-itemLabel
Run Code Online (Sandbox Code Playgroud)

Deb*_*anB 4

你已经足够接近了。事实上,您的整个程序只有一个问题,如下所示:

  • 由于第一个匹配元素是隐藏元素,因此不会将带有文本的可见元素唯一标识xpath_runall = "//li[@data-command='notebook:run-all-below']"运行选定单元格和所有下方”

其他注意事项

更多优化:

  • 标识为的元素xpath = "//button[@title='Save the notebook contents and create checkpoint']"可点击元素。所以你可以使用EC代替presence_of_element_located()element_to_be_clickable()

  • 一旦元素通过EC返回,您就可以在同一行element_to_be_clickable()调用。click()

  • 将带有文本的元素标识为“运行所选单元格和下面的所有内容”的为:

    //li[@data-command='notebook:run-all-below']//div[@class='lm-Menu-itemLabel p-Menu-itemLabel' and text()='Run Selected Cell and All Below']
    
    Run Code Online (Sandbox Code Playgroud)
  • 由于应用程序是通过JavaScript构建的,因此您需要使用ActionChains


解决方案

您的优化解决方案将是:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    
    driver = webdriver.Firefox(executable_path=r'C:\WebDrivers\geckodriver.exe')
    driver.get("https://mybinder.org/v2/gh/jupyterlab/jupyterlab-demo/master?urlpath=lab/tree/demo")
    WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.XPATH, "//button[@title='Save the notebook contents and create checkpoint']")))
    print("Page loaded")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Run']"))).click()
    print("Clicked on Run")
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@data-command='notebook:run-all-below']//div[@class='lm-Menu-itemLabel p-Menu-itemLabel' and text()='Run Selected Cell and All Below']")))
    ActionChains(driver).move_to_element(element).click(element).perform()
    print("Clicked on Run Selected Cell and All Below")
    
    Run Code Online (Sandbox Code Playgroud)
  • 控制台输出:

    Page loaded
    Clicked on Run
    Clicked on Run Selected Cell and All Below
    
    Run Code Online (Sandbox Code Playgroud)