简体   繁体   English

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

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

Update: here's a screenshot of the frame I'm having trouble switching to: frame that cannot be located 更新:这是我无法切换到的框架的屏幕截图: 无法找到的框架

I've tried selecting this element multiple different ways but always get the "unable to locate" error. 我尝试以多种不同方式选择此元素,但始终会收到“无法定位”错误。

I can easily locate elements on most pages but I suspect this html requires an extra step or two to get at the link. 我可以轻松地在大多数页面上找到元素,但是我怀疑此html需要额外的一两个步骤才能到达链接。

Here's the html--the link is in the 3rd line: 这是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>

This element is on a page behind a login page here which I have no problem logging into using the following code: 此元素位于登录页面后面的页面上 ,使用以下代码登录时我没有问题:

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)

After this runs, the page that contains the link I'm having trouble locating loads. 运行此命令后,包含链接的页面在查找负载时遇到问题。 I've tried finding by id, xpath, css selector, link text, and partial link text without success. 我尝试通过id,xpath,css选择器,链接文本和部分链接文本查找失败。

I'm hoping someone can look at the html at the top of my question and tell me how they'd go about locating the link in the third line so that I can then issue a click() on it. 我希望有人可以查看问题顶部的html,并告诉我他们如何在第三行中找到链接,以便随后可以在其上发出click()。

screenshot of page behind login with element circled and arrow pointing to html 登录后页面的屏幕截图,其中带圆圈的元素和指向html的箭头

Latest Update w/error: 带有错误的最新更新:

>>> 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

The element you are looking for is inside a frame, so first you need to switch to that frame and then search for the element like this. 您要查找的元素在框架内,因此首先需要切换到该框架,然后搜索这样的元素。

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

Answering my own question here: 在这里回答我自己的问题:

The problem was with the Firefox (geckodriver) webdriver. 问题出在Firefox(geckodriver)网络驱动程序上。 I encountered no issues when using the Chrome driver. 使用Chrome驱动程序时没有遇到任何问题。

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

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