【问题标题】:How to Handle Popup Windows That Occur within Selenium Python如何处理 Selenium Python 中出现的弹出窗口
【发布时间】:2020-06-09 14:38:06
【问题描述】:

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

<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'] 你可以找到完整的代码here。 主要方面是我必须在File ID 字段中输入TSI,然后在我的键盘上点击enter 以填充我需要的正确导入实用程序。一旦我这样做了,导入实用程序就会过滤掉,我需要选择一个特定的File 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)

【问题讨论】:

  • >"但是我认为我的代码在弹出窗口时不能正确处理它" 但是当您运行代码时会发生什么?什么都没有/错误?
  • 不,我得到了错误。让我贴在这里
  • 不,我收到错误,我会将其添加到问题中
  • @0buz 我已将其添加到问题的底部。
  • iframe 里面的元素有没有机会?如果您可以发布html源代码会很好。但是,如果您的元素确实在 iframe 中,则需要先切换到 iframe,然后才能找到该字段。使用driver.switch_to.frame(&gt;&gt;iframe name or id here) 做到这一点

标签: python-3.x selenium selenium-webdriver automation browser-automation


【解决方案1】:

所以我能够通过使用 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)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    相关资源
    最近更新 更多