Python Selenium - 查找表元素 <td> 并获取它们的值 - 但新页面加载了一秒钟,然后由于未知原因返回上一个页面

Python Selenium - find table elements <td> and get their values - but new page loads for a second and then for unknown reason return to previus page

提问人:José Souza 提问时间:6/3/2023 最后编辑:Ajeet VermaJosé Souza 更新时间:6/4/2023 访问量:46

问:

当我手动单击图像时,新页面会加载,并且新页面是稳定的。

当我在 python 中通过 selenium 单击时,新页面加载了几秒钟,然后出于未知原因自动返回上一页,因此我无法捕获新页面的元素。

第一次点击是这个图片:

<span class="option center" id="FXS_edit">
    <img src="../img/edit.png" title="Editar">
</span>

新页面有一个名为“extensions_table”的表,我想检索“value='558131835426'”

<table id="extensions_table" class="form">
                    <thead>
                    <tr>
                    </tr>
                    </thead>
                    <tbody id="kextensionsitemsform">
<tr class="highlight">
  
        <td><input type="checkbox" value="0" checked="">0</td>
        <td><input type="text" value="558131835426" maxlength="15" autocomplete="off"></td>
        <td><input class="user_ext" type="text" value="558131835426"></td>
        <td><input class="pwd_ext" type="password" value="Asd123!."></td>
</tr>

我的代码:

chrome_options = Options()
chrome_options.add_argument("--start-maximized")
chrome_options.BinaryLocation = os.getcwd() + os.sep + "chrome.exe" + \
    os.sep + "chrome.exe"

driver = webdriver.Chrome(options=chrome_options)

driver.get("http://login:passwd@IP Address") 

driver.find_element(By.CSS_SELECTOR, "#FXS_edit").click()

sleep(2)

table_id = driver.find_element(By.XPATH,'/html/body/div[1]/div[3]/div/div[5]/fieldset/div[2]/div[1]/div/form/table')
/html/body/div[1]/div[3]/div/div[5]/fieldset/div[2]/div[1]/div/form/table

#or

table_id = driver.find_element(By.ID, "extensions_table")

我收到消息:

NoSuchElementException: Message: no such element: Unable to locate element

因为新页面不再出现在屏幕上,它已经返回到上一页

如何使新页面稳定,就像手动运行页面一样?

我尝试了不同的方法来单击img,但相同的模式重复。

python selenium-webdriver 网页抓取 html-table nosuchelementexception

评论

0赞 larsks 6/3/2023
我不确定我们是否可以在不与目标网站交互的情况下回答这个问题。听起来问题出在网站的行为上。
0赞 José Souza 6/3/2023
该页面只能在本地网络上访问,可能只能通过远程访问
0赞 José Souza 6/4/2023
我可能是错的,我刚刚开始编程,但是,如果是网站上的问题行为,为什么它在手动按下时会起作用
0赞 José Souza 6/4/2023
我发现,问题与我使用的身份验证方法有关。

答:

0赞 José Souza 6/4/2023 #1

我发现,问题与我使用的身份验证方法有关。

当我访问该网站时,会弹出一个警报框,询问登录名和密码。

我在使用驱动程序获取方法时将身份验证放在 URL 中。

driver.get("http://login:passwd@IP Address")

我通过在身份验证警报弹出窗口中使用 pyautogui(单击和键入)更改了它,以及稍后发生的问题(页面加载然后返回上一页),单击 img 后,不再发生,我真的不知道这两件事之间有什么关系。

pyautogui.click(670,188,duration=1)
pyautogui.typewrite("login")
pyautogui.click(673,230,duration=1)
pyautogui.typewrite("password")

我想知道是否有另一种方法可以在警报弹出时使用 selenium 进行身份验证