Reputation: 97
I'm trying to iterate through web elements, find elements with an expandable menu, then save that information in the menu. 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.
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.
Upvotes: 1
Views: 155
Reputation: 3826
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.
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
from selenium.webdriver.support import expected_conditions as EC
and wait was set up with
wait = WebDriverWait(driver, 15)
Upvotes: 2