繁体   English   中英

如何在 Selenium (Python) 中访问此元素

[英]How do I access this element in Selenium (Python)

我正在尝试从即时搜索下拉菜单中获取链接。 例如,当我键入“AA”时,我想要第一个选项,但我似乎无法获得 HTML id,因此无法单击它。 如何使用 Selenium 访问此结果?

搜索截图

网址是: https://www.sec.gov/edgar/searchedgar/companysearch.html

我也开始在 DOM 中搜索,但没有找到与提示框相关的任何内容。 然后我去“Chrome Dev tool - Network tab”查看服务器响应。 在那里我发现他们正在生成一些响应,然后我进入了 javascript 代码并找到了这些 CSS 选择器/类名选择器。 我用来提取文本并找到第一个元素并单击它。

希望这会有所帮助。

请尝试以下代码 -

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5)
action = ActionChains(driver)

driver.get("https://www.sec.gov/edgar/searchedgar/companysearch.html")

SearchBox = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@id='company']")))
action.move_to_element(SearchBox).click().send_keys('AA').perform()

# Wait till the results with hint populated (I think with the help of javascript they are doing)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'table.smart-search-entity-hints')))

# This will print all the List items from the hint box
print(driver.find_element_by_css_selector('table.smart-search-entity-hints').text)

# To find the first item to click i have used 'find_elements_by_class_name' you can also use
# find_elements_by_class_name to get a item collection (Have not tested it..)

driver.find_element_by_class_name('smart-search-selected-hint').click()

暂无
暂无

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

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