首页 > 解决方案 > 如何处理 Selenium Python 中出现的弹出窗口

问题描述

所以我有一个问题,我试图在没有 API 的应用程序上自动导入。结果,我必须点击 30 次导航才能达到我想要的效果(夸张)。但是,我试图基本上自动化允许我上传特定文件的点击。结果,我几乎到了必须选择要导入文件的特定测试版本的部分。我需要做一个字段send_keys才能找到我必须上传的正确导入版本。字段元素看起来像这样

<input class="lookupInput" type="text" name="brTestScoreImportLookupInput" id="brTestScoreImportLookupInput" style="width: 100px;" tabindex="1" onkeydown="return lookupKeyPressed(event,&quot;&quot;,&quot;simptbrws000.w&quot;)" origvalue="" det="true" aria-labelledby="" autocomplete="off">

但是,我认为我的代码没有正确处理窗口,因为它从先前的选择中弹出。我需要更新的字段可以在我上传的图片中找到: 在此处输入图像描述 此外,该字段的 XPATH 是您可以在此处//*[@id='brTestScoreImportLookupInput'] 找到完整代码。主要方面是我必须输入该字段,然后点击我的键盘来填充我需要的正确导入实用程序。一旦我这样做了,导入实用程序就会过滤掉,我需要选择一个特定的: 。TSIFile IDenterFile ID在此处输入图像描述

应该控制这个的主要代码:

# Click on Test Score Import Wizard - TW
# Test Wizard XPATH = //a[@id='tree1-3-link']/span
element = WebDriverWait(browser, 20).until(
    EC.element_to_be_clickable((By.XPATH, "//a[@id='tree1-3-link']/span")))
element.click();

# Send test_upload and Send Keys
# Field XPATH = //*[@id='brTestScoreImportLookupInput']
test_lookup = browser.find_element_by_id("brTestScoreImportLookupInput")
test_lookup.send_keys(test_upload)

如果您想访问链接指向存储库代码,请单击此处上方的此处。任何帮助将不胜感激。

    Traceback (most recent call last): File ".\skyward_collegeboard_TSI_import.py", line 115, in
    <module> test_lookup = browser.find_element_by_id("brTestScoreImportLookupInput") File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id return self.find_element(by=By.ID, value=id_) File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py",
      line 976, in find_element return self.execute(Command.FIND_ELEMENT, { File "C:\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Python38\lib\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":"css selector","selector":"[id="brTestScoreImportLookupInput"]"}
      (Session info: chrome=80.0.3987.122)

标签: python-3.xseleniumselenium-webdriverautomationbrowser-automation

解决方案


因此,我能够通过使用 selenium 和 pynput 的以下方法来完成此操作。

# Browser Switches to Window
WebDriverWait(browser,10).until(EC.number_of_windows_to_be(2))
browser.switch_to.window(browser.window_handles[-1])

# Send test_upload and oend Keys
# Field XPATH = //*[@id='brTestScoreImportLookupInput']
test_lookup = browser.find_element_by_id("brTestScoreImportLookupInput")
test_lookup.send_keys(test_upload)

# Press and Release Enter Key
keyboard.press(Key.enter)
keyboard.release(Key.enter)

基本上我不得不切换到那个弹出窗口。


推荐阅读