How to find the x path of an element when the python selenium script clicks a button which in turn opens in a new window (element is in new window)

huangapple go评论56阅读模式
英文:

How to find the x path of an element when the python selenium script clicks a button which in turn opens in a new window (element is in new window)

问题

The python(selenium) script fails when a new chrome window opens upon clicking a button. I have passed the x-path of the element from the new window(extracted the x-path manually) in my script. The script runs and fails with an error selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="3131"]"}

如何解决这个问题?

期望有一种方法将脚本传递到新打开的窗口并找到我打算定位的元素的 x-path。

英文:

The python(selenium) script fails when a new chrome window opens upon clicking a button. I have passed the x-path of the element from the new window(extracted the x-path manually) in my script. The script runs and fails with an error selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="3131"]"}

How can I resolve this issue ?

Expecting a method to pass the script to newly opened window and find the x-path of the element I intent to locate

答案1

得分: 2

以下是翻译好的部分:

Python:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

with webdriver.Firefox() as driver:
    # 打开URL
    driver.get("https://seleniumhq.github.io")

    # 设置等待
    wait = WebDriverWait(driver, 10)

    # 存储原始窗口的ID
    original_window = driver.current_window_handle

    # 检查是否已经打开其他窗口
    assert len(driver.window_handles) == 1

    # 单击打开在新窗口中的链接
    driver.find_element(By.LINK_TEXT, "new window").click()

    # 等待新窗口或选项卡
    wait.until(EC.number_of_windows_to_be(2))

    # 循环查找新窗口句柄
    for window_handle in driver.window_handles:
        if window_handle != original_window:
            driver.switch_to.window(window_handle)
            break

    # 等待新标签页完成加载内容
    wait.until(EC.title_is("SeleniumHQ Browser Automation"))
英文:

The selenium documentation shows how to switch to a new window quite well and once you switched to the new window you just work as if you were in the main window.

Python:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

with webdriver.Firefox() as driver:
    # Open URL
    driver.get("https://seleniumhq.github.io")

    # Setup wait for later
    wait = WebDriverWait(driver, 10)

    # Store the ID of the original window
    original_window = driver.current_window_handle

    # Check we don't have other windows open already
    assert len(driver.window_handles) == 1

    # Click the link which opens in a new window
    driver.find_element(By.LINK_TEXT, "new window").click()

    # Wait for the new window or tab
    wait.until(EC.number_of_windows_to_be(2))

    # Loop through until we find a new window handle
    for window_handle in driver.window_handles:
        if window_handle != original_window:
            driver.switch_to.window(window_handle)
            break

    # Wait for the new tab to finish loading content
    wait.until(EC.title_is("SeleniumHQ Browser Automation"))

huangapple
  • 本文由 发表于 2023年3月1日 12:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定