python 无法使用xpath找到元素

velaa5lx  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(143)

我试图登录到公司网站,然后自动化我的工作流程在网站上。到目前为止,我使用Python Selenium创建的脚本将登录,但在此之后,它无法找到我想要单击的元素。注意我在jupyter notebook上这样做。
这是我用来自动化工作流程的selenium/Python代码:

from selenium import webdriver
from selenium.webdriver.common.by import By

# Create an instance of the web driver
driver = webdriver.Chrome()  # Change to the appropriate web driver (e.g., Firefox(), 
Safari(), etc.)

# Open a webpage
driver.get('https://3gissvrdev.xxxxxxxxx.com/3GISWebJS/')

# Find an element and click it
user_name =  driver.find_element("id", "usernameField") 
user_name.send_keys("CarpenCD")

pw=driver.find_element(By.ID, "passwordField")
pw.send_keys("xxxxxxx")

SigninButton=driver.find_element(By.XPATH,"//*[@id=\"signinButton\"]")
SigninButton.click()

#use this section of code to give time for web page to load after logging on
in1=input('are you ready to move on')
if in1 == 'y':
Query=driver.find_element(By.XPATH,"//*[@id=\"d481e7896cf443e3aae933d31fc5a1d4\"]/paper-button/span")
Query.click()

# Close the browser
#driver.quit()

这是我想点击下面看到的按钮的html脚本:

然后我复制上图中高亮显示的代码的xpath,并将该路径放入selenium/python脚本中,用于代码的查询变量,如上面的第一个示例所示。
可能导致下面所示错误的原因。一切工作,直到登录按钮被点击。注意:登录点击不会打开不同的页面/选项卡,登录后域预登录是相同的

NoSuchWindowException                     Traceback (most recent call last)
<ipython-input-2-a0b3b1283d5d> in <module>
     23 in1=input('are you ready to move on')
     24 if in1 == 'y':
---> 25     Query=driver.find_element(By.XPATH,"//* 
[@id=\"d481e7896cf443e3aae933d31fc5a1d4\"]/paper-button/span")
     26     Query.click()
     27 

~\AppData\Roaming\Python\Python39\site-packages\selenium\webdriver\remote\webdriver.py 
in find_element(self, by, value)
    829             value = f'[name="{value}"]'
    830 
--> 831         return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value}) 
["value"]
    832 
    833     def find_elements(self, by=By.ID, value: Optional[str] = None) -> 
List[WebElement]:

~\AppData\Roaming\Python\Python39\site-packages\selenium\webdriver\remote\webdriver.py 
in execute(self, driver_command, params)
    438         response = self.command_executor.execute(driver_command, params)
    439         if response:
--> 440             self.error_handler.check_response(response)
    441             response["value"] = self._unwrap_value(response.get("value", None))
    442             return response

~\AppData\Roaming\Python\Python39\site- 
packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    243                 alert_text = value["alert"].get("text")
    244             raise exception_class(message, screen, stacktrace, alert_text)  # 
type: ignore[call-arg]  # mypy is not smart enough here
--> 245         raise exception_class(message, screen, stacktrace)

NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
  (Session info: chrome=113.0.5672.127)
Stacktrace:
Backtrace:
    GetHandleVerifier [0x00BA8893+48451]
    (No symbol) [0x00B3B8A1]
    (No symbol) [0x00A45058]
    (No symbol) [0x00A2D073]
    (No symbol) [0x00A8DEBB]
    (No symbol) [0x00A9BFD3]
    (No symbol) [0x00A8A0B6]
    (No symbol) [0x00A67E08]
    (No symbol) [0x00A68F2D]
    GetHandleVerifier [0x00E08E3A+2540266]
    GetHandleVerifier [0x00E48959+2801161]
    GetHandleVerifier [0x00E4295C+2776588]
    GetHandleVerifier [0x00C32280+612144]
    (No symbol) [0x00B44F6C]
    (No symbol) [0x00B411D8]
    (No symbol) [0x00B412BB]
    (No symbol) [0x00B34857]
    BaseThreadInitThunk [0x76717D59+25]
    RtlInitializeExceptionChain [0x77DBB74B+107]
    RtlClearBits [0x77DBB6CF+191]
pkwftd7m

pkwftd7m1#

从上面的错误,它说,您正在访问的窗口,而登录和您试图访问的窗口是不同的,前一个被关闭。因此,要切换到当前活动窗口,请使用以下代码:

def switch_window(index: int):
    driver.switch_to.window(driver.window_handles[index])

将索引传递为-1,这样你的驱动程序将指向最后打开的窗口,你可以访问其中的元素。登录后,调用并使用此函数。

相关问题