首页 > 解决方案 > qbittorrent 应用程序的 Web UI 上的 Selenium 自动化“无法定位元素”错误

问题描述

我正在尝试使用 BitTorrent Web UI 自动下载带有磁力链接的电影。我可以单击“添加 torrent 链接”按钮,弹出窗口确实出现,但之后,代码失败,因为它无法找到需要添加 torrent 链接的元素。当我尝试输入文件位置时,也会出现同样的问题。我试过time.sleep但没有运气。

我的代码片段:

def torrent(path, n):
    #link to web UI
    driver.get("http://127.0.0.1:8080/")
    #default login credentials
    username = driver.find_element_by_xpath("//*[@id='username']")
    username.send_keys("admin")
    password = driver.find_element_by_xpath("//*[@id='password']")
    password.send_keys("adminadmin")
    time.sleep(2)
    driver.find_element_by_xpath("//*[@id='login']").click()
    time.sleep(2)
    #the elements I am trying to find
    driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/a[1]").click()
    time.sleep(5)
    location = driver.find_element_by_xpath("/html/body/form/div/fieldset/table/tbody/tr[2]/td[2]")
    location.send_keys(path)
    input = driver.find_element_by_xpath("/html/body/form/div/textarea")
    i = 0
    while i <= n-1:
        input.send_keys(list_torrent[i])

如果您需要任何其他信息,请告诉我。我已经尝试使用 BitTorrent API,但没有运气。HTML 页面有一个隐藏的溢出,但这应该不是问题,因为我单击了应该使我的代码可见的元素。提前致谢。

标签: pythonhtmlpython-3.xselenium

解决方案


看起来它在 iframe 中。

切换到iframe这样的:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH," Ifrmae xpath here"))) 

然后在这里做其他的事情:

完成 iframe 后,将其切换回 default_content如下状态:

driver.switch_to.default_content()

推荐阅读