简体   繁体   中英

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.

Here's the html--the link is in the 3rd line:

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

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.

screenshot of page behind login with element circled and arrow pointing to 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. I encountered no issues when using the Chrome driver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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