繁体   English   中英

如何在Selenium中选择此元素(python)

[英]How to select this element in Selenium (python)

更新:这是我无法切换到的框架的屏幕截图: 无法找到的框架

我尝试以多种不同方式选择此元素,但始终会收到“无法定位”错误。

我可以轻松地在大多数页面上找到元素,但是我怀疑此html需要额外的一两个步骤才能到达链接。

这是html-链接在第三行:

<div class="menu_bg">
    <ul class="menu">
        <li id="retrieve"><a href="https://s1.ebridge.com/ebridge/3.0/retrieve/search.aspx?search=new&amp;guid=4ae139ed-287a-4087-8fe0-56ff3683e160" id="aView" onclick="clickme(this,'retrieve')" target="main">Retrieve</a></li>
        <li id="help"><a id="aSupport" onclick="clickme(this,'reports')" target="main" href="Support/support_main.aspx?guid=4ae139ed-287a-4087-8fe0-56ff3683e160">Support</a></li>
        <li style="float: right;">
            <span id="cabnm" class="cabinet_box">PinCHD</span>
        </li>
    </ul>
</div>

此元素位于登录页面后面的页面上 ,使用以下代码登录时我没有问题:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import re
import pandas as pd
import os
from selenium.webdriver.common.by import By
url = "https://s1.ebridge.com/ebridge/3.0/default.aspx?1"
driver = webdriver.Firefox(executable_path="/Applications/Postgres.app/Contents/Versions/11/bin/geckodriver")
driver.implicitly_wait(30)
driver.get(url)
username = driver.find_element_by_name("tbUserName")
username.clear()
username.send_keys("public")
password = driver.find_element_by_name("tbPassword")
password.clear()
password.send_keys("public")
file_cabinet = driver.find_element_by_name("tbFileCabinet")
file_cabinet.clear()
file_cabinet.send_keys("PINCHD")
file_cabinet.send_keys(Keys.RETURN)

运行此命令后,包含链接的页面在查找负载时遇到问题。 我尝试通过id,xpath,css选择器,链接文本和部分链接文本查找失败。

我希望有人可以查看问题顶部的html,并告诉我他们如何在第三行中找到链接,以便随后可以在其上发出click()。

登录后页面的屏幕截图,其中带圆圈的元素和指向html的箭头

带有错误的最新更新:

>>> from selenium import webdriver
>>> from selenium.webdriver.common.keys import Keys
>>> from bs4 import BeautifulSoup
>>> import re
>>> import pandas as pd
>>> import os
>>> from selenium.webdriver.common.by import By
>>> url = "https://s1.ebridge.com/ebridge/3.0/default.aspx?1"
>>> driver = webdriver.Firefox(executable_path="/Applications/Postgres.app/Contents/Versions/11/bin/geckodriver")
>>> driver.implicitly_wait(30)
>>> driver.get(url)
>>> username = driver.find_element_by_name("tbUserName")
>>> username.clear()
>>> username.send_keys("public")
>>> password = driver.find_element_by_name("tbPassword")
>>> password.clear()
>>> password.send_keys("public")
>>> file_cabinet = driver.find_element_by_name("tbFileCabinet")
>>> file_cabinet.clear()
>>> file_cabinet.send_keys("PINCHD")
>>> file_cabinet.send_keys(Keys.RETURN)
>>> driver.implicitly_wait(15)
>>> driver.switch_to.frame("header")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/switch_to.py", line 89, in frame
    self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchFrameException: Message: Unable to locate frame: b91c4c93-d8fd-e543-9ce2-a58ade0081db

>>> retrieve = driver.find_element_by_xpath("//*[@id="aView"]")
  File "<stdin>", line 1
    retrieve = driver.find_element_by_xpath("//*[@id="aView"]")
                                                          ^
SyntaxError: invalid syntax
>>> retrieve.click()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'retrieve' is not defined

您要查找的元素在框架内,因此首先需要切换到该框架,然后搜索这样的元素。

driver.switch_to.frame('header')  #Match by name of the frame
my_element = driver.find_element_by_xpath('//*[@id="aView"]')
my_element.click()

在这里回答我自己的问题:

问题出在Firefox(geckodriver)网络驱动程序上。 使用Chrome驱动程序时没有遇到任何问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM