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

时间:2019-01-22 02:12:13

标签: python html selenium

更新:这是我无法切换到的帧的屏幕截图: frame that cannot be located

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

我可以轻松地在大多数页面上找到元素,但是我怀疑此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>

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

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,并告诉我他们如何在第三行中找到链接,以便我可以点击()。

screenshot of page behind login with element circled and arrow pointing to 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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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