python - 单击由 JavaScript 使用 Python Webdriver 编写的数据绑定(bind)下的按钮

标签 python selenium-webdriver

我可以登录网站,但不知道如何点击“下载为 CSV”。 我尝试过 XPATH 和 CSS 选择器。

这是 HTML 的图片。 enter image description here

仪表板与许多小部件结合在一起。不知道这是否会影响什么。 enter image description here 如果您需要更多信息,请告诉我。谢谢:)

下面是代码

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)

弹出错误。

文件“f:\User\Downloads\yardi.py”,第 57 行,位于

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

文件“F:\Anaconda3\lib\site-packages\selenium\webdriver\support\wait.py”,第 80 行,直到引发 TimeoutException(消息、屏幕、堆栈跟踪) selenium.common.exceptions.TimeoutException:消息:

最佳答案

看起来小部件总是在同一个位置,如果您在 Windows 上运行它,我通常会尝试使用 X/Y 坐标与 AppRobotic 等宏工具进行单击。如果这是页面加载缓慢的问题,我通常会尝试停止页面加载,并与页面进行一些交互,这样的操作应该适合您:

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)

关于python - 单击由 JavaScript 使用 Python Webdriver 编写的数据绑定(bind)下的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57046606/

相关文章:

java - 如何使用java获取下个月的前三个日期

python - pd.to_datetime 小时和秒

javascript - Dropzone 上传到 Flask 应用后不会重定向

python - Windows 10 上的 opencv python 相机权限问题

python - tensorflow 未找到错误

java - 如何在 selenium webdriver 中选择单选按钮?动态选择单选按钮

python - mysql python 从特定行检索特定列

python - Xpath Selenium-如何通过子文本定位元素

java - 可以在 MS TFS 中执行 Java Maven 项目吗?

node.js - 错误消息: Unable to find element with xpath in phantomsJS