简体   繁体   English

在 Python 中使用 Selenium 从下拉菜单中检索数据的最佳方法是什么?

[英]What's the best way to retrieve data from a drop down menu using Selenium in Python?

I'm trying to iterate through web elements, find elements with an expandable menu, then save that information in the menu.我正在尝试遍历 Web 元素,找到带有可扩展菜单的元素,然后将该信息保存在菜单中。 Here's my code:这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

#open chrome tab
driver = webdriver.Chrome(executable_path='C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe')

#open the url
driver.get('https://app.smartsheet.com/sheets/Phfqpp2qpvrfjw9jPggm8fRfWgqgj2mgXv5xjh71?view=grid')

#find email input box
email = driver.find_element_by_xpath('/html/body/table/tbody/tr[1]/td[2]/form/div[2]/div[3]/div[2]/div/table/tbody/tr[1]/td/div/table/tbody/tr[1]/td/input')

#type username
email.send_keys('USERNAME')
email.send_keys(Keys.RETURN)

time.sleep(1)

#find password input box
password = driver.find_element_by_xpath('/html/body/table/tbody/tr[1]/td[2]/form/div[2]/div[3]/div[2]/div/table/tbody/tr[1]/td/div/table/tbody/tr[2]/td/input')

#type password
password.send_keys('PASSWORD')
password.send_keys(Keys.RETURN)

time.sleep(12)

#find activity log button
activity_log = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div[3]/div[5]/div[3]/div/div/button[6]')

#click the activity log button
activity_log.click()

time.sleep(1)

#find input box for start date
start_date = driver.find_element_by_xpath('/html/body/div[1]/div[6]/div/div[1]/div[1]/span[1]/div[3]/input')

#type in necessary date
start_date.clear()
start_date.send_keys('06/07/21')

#apply the date change
apply = driver.find_element_by_xpath('/html/body/div[1]/div[6]/div/div[1]/div[1]/span[1]/span[6]/div/div')

apply.click()

#find drop down menu carets
entries = driver.find_elements_by_class_name('clsHistoryItemCaret')

This signs into my account and opens up the activity log correctly, which contains the data I need.这会登录我的帐户并正确打开活动日志,其中包含我需要的数据。 However, it falls apart when I try to find the expandable items by class name.但是,当我尝试按类名查找可扩展项时,它会崩溃。 This is the HTML view of the button that drops down the menu with the information I need.这是下拉菜单的按钮的 HTML 视图,其中包含我需要的信息。

活动日志的 HTML 视图

The class name search isn't finding any elements.类名搜索未找到任何元素。 So, I was wondering if there was something wrong I was doing or, better yet, there's a more efficient way to do this.所以,我想知道我是否做错了什么,或者更好的是,有一种更有效的方法来做到这一点。

The issue was that the dropdown was not loaded yet after the click, and adding a time.sleep afterwards allowed the web elements to be found.问题是点击后下拉菜单尚未加载,然后添加 time.sleep 允许找到网络元素。

Rather than sleep, you may want to find a good locator for the close button and wait for it to be present.与其睡觉,您可能还想为关闭按钮找到一个好的定位器,然后等待它出现。 Those sleeps start to add up.这些睡眠开始累积。

You may want to start using expressions like:您可能想开始使用以下表达式:

wait.until(EC.element_to_be_clickable((By.XPATH, 'your xpath here'))).click()

where EC was imported as其中 EC 被导入为

from selenium.webdriver.support import expected_conditions as EC

and wait was set up with并等待设置为

wait = WebDriverWait(driver, 15)

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

相关问题 使用python从下拉菜单中抓取所有动态生成的数据的最佳方法 - Best way to scrape all the dynamically generated data from a drop down menu using python 使用Selenium和python从下拉菜单中选择一个选项 - seleting an option from a drop down menu using selenium and python 如何使用 Selenium 和 Python 从下拉菜单中的 select 项目? - How to select item from drop down menu using Selenium with Python? 在python中使用硒从下拉菜单中选择多个选项 - Selecting multiple options from drop down menu using selenium in python Python Selenium - 下拉菜单 - Python Selenium - Drop down menu Selenium Python 我选择下拉项是最好的方法吗? - Selenium Python Am I selecting a drop down item the best way? Selenium 有没有办法从谷歌表单(python)的下拉菜单中选择一个选项 - Is there a way in Selenium to select an option from a drop down menu in google forms (python) 从 python 的项目中检索模块路径的最佳方法是什么? - What's the best way to retrieve the module path from the project in python? Python & Selenium:分层select数据的最佳方法是什么? - Python & Selenium: what's the best way to hierarchically select data from html elements? 使用 selenium Python 库单击下拉菜单中的项目 - Click an item in a drop-down menu using selenium Python library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM