如何在selenium python中传递Tab键和下箭头键

i7uq4tfw  于 2022-12-18  发布在  Python
关注(0)|答案(1)|浏览(262)

我已经写了一个代码,登录到一个网页,并试图下载一个文件。但登录后,下载之前,它需要选择一个特定的报告“通过贷款市场”通过单选按钮窗口,有没有人能帮忙做些什么?我想这样做,按两次Tab键,然后点击向下箭头键,最后点击下载按钮,但我的代码written不起作用,你能帮帮忙吗?

from selenium import webdriver
from selenium.webdriver.common.action_chains import Actionchains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)

wait = WebDriverWait(driver, 20)

url = "https://www.osaka.com"
driver.get(url)

wait.until(EC.element_to_be_clickable((By.NAME, "username"))).send_keys("*****")
wait.until(EC.element_to_be_clickable((By.NAME, "password"))).send_keys("******")
wait.until(EC.element_to_be_clickable((By.TAG_NAME, "button"))).click()
ActionChains(driver).send_keys(Keys.Tab*2).perform()
z31licg0

z31licg01#

这可以通过使用XPath来完成。
您可以根据子元素td的文本定位父元素tr,然后访问第二个子元素input
我希望您看到这些元素都在iframe中,您需要切换到iframe中以访问那里的元素,并在您想要访问iframe之外的元素时切换回默认内容。
下面的代码是有效的:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 30)

url = "https://products.ihsmarkit.com/home/index.jsp#LOANS.DOWNLOAD.download"
driver.get(url)

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[type='email']"))).send_keys("***")
wait.until(EC.element_to_be_clickable((By.ID, "submitBtn"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[type='password']"))).send_keys("***")
wait.until(EC.element_to_be_clickable((By.ID, "submit"))).click()

wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "MFrame")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//tr[./td[text()='Past Loan Market']]//input"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='downloadButton']"))).click()

相关问题