1
votes

I'm able to login to a website but I couldn't figure it out how to click "Download as CSV". I've tried XPATH and CSS Selector.

Here is the picture of the HTML. enter image description here

The dashboard is combined with many widgets. Don't know if that would impact anything. enter image description here Please let me know if you need more information. Thank you : )

Below is code

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

#### location of chrome 
driver = webdriver.Chrome(r'F:\User\Downloads\chromedriver.exe')

#### log in website 1
driver.get('https://www.yardipca.com/39444siteminderlive/')

### 1st door ( login& password)
name_1st = driver.find_element_by_name('USER')
name_1st.clear()
name_1st.send_keys('my_account')

passowrd_1st = driver.find_element_by_name('PASSWORD')
passowrd_1st.clear()
passowrd_1st.send_keys('my_password')

### login botton
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH,"/html/body/form/div/center/table[2]/tbody/tr[3]/td[2]/table/tbody/tr[8]/td[2]/input[6]"))).click()
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.LINK_TEXT,"P260 Live System (SAN/HOC)"))).click()
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="acceptButton"]'))).click()

time.sleep(10)




#### log in website 2
driver.get('https://www.yardiasptx11.com/39444p260livehoc/jsp/index.jsp')

### 2nd door ( login& password)
name_2nd = driver.find_element_by_name('userId')
name_2nd.send_keys('my_account')

passowrd_2nd = driver.find_element_by_name('password')
passowrd_2nd.send_keys('my_password')

### login botton
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//[@id="standardLoginForm"]/div[3]/button'))).click()
time.sleep(5)

### Dropdown button
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="widget_937242"]/div[1]/div[1]'))).click()

# //*[@id="widget_937242"]/div[1]/div[1]
# //*[@id="widget_937242"]/div[1]/div[1]/a
# //*[@id="widget_937242"]/div[1]/div[1]/a/span

### download file
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#widget_937242 > div.panel-heading.ui-sortable-handle > div.btn-group.pull-right.print-hide.open > ul > li:nth-child(2) > a'))).click()



time.sleep(10)

The error popped up.

File "f:\User\Downloads\yardi.py", line 57, in

WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="widget_937242"]/div1/div1'))).click()

File "F:\Anaconda3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

2
Please read why a screenshot of code is a bad idea. Paste the code and properly format it instead.JeffC

2 Answers

1
votes

It looks like the widget is always in the same place, I typically try clicking by using X/Y coordinates with a macro tool like AppRobotic if you're running this on Windows. If it's an issue with the page loading slowly, I usually try stopping the page load, and interacting with the page a bit, something like this should work for you:

import win32com.client
from win32com.client import Dispatch
x = win32com.client.Dispatch("AppRobotic.API")
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://www.google.com')
# wait 20 seconds
x.Wait(20000)
# scroll down a couple of times in case page javascript is waiting for user interaction
x.Type("{DOWN}")
x.Wait(2000)
x.Type("{DOWN}")
x.Wait(2000)
# forcefully stop pageload at this point
driver.execute_script("window.stop();")
# if clicking Download as CSV button with Selenium still does not work here, use screen coordinates
x.MoveCursor(xCoord, yCoord)
x.MouseLeftClick
x.Wait(2000)
0
votes

It's within the iframe so I switch the iframe that I want o act on.