【问题标题】:How to upload file using Selenium with Python如何使用 Selenium 和 Python 上传文件
【发布时间】:2020-12-05 16:15:02
【问题描述】:

我想通过下面的链接附加文件。如何使用 selenium python 做到这一点?

我的代码:

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

driver=webdriver.Chrome(executable_path="C:\Program Files (x86)\Drivers\chromedriver.exe")

driver.get("https://www.tascoutsourcing.com/job-openings/quality-assurance-engineer/")

driver.maximize_window()

time.sleep(10)

driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)

driver.find_element_by_xpath("//*[@id='content']/div[2]/div[2]/div/div/div/div[1]/div/div/div/button").click()

driver.find_element_by_xpath("//*[@id='content']/div[2]/div[2]/div/div/div/section/div/form/div[3]/div[3]/div").click()

【问题讨论】:

  • 你想附加什么?你能指定吗

标签: python python-3.x selenium upload


【解决方案1】:

paul,请尝试运行以下代码 - 我在我的系统中运行了相同的代码,它对我有用。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)

driver.get("https://www.tascoutsourcing.com/job-openings/quality-assurance-engineer/")

# Click on Apply Button
Apply_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Apply Now']/parent::button")))
action.move_to_element(Apply_btn).click().perform()

time.sleep(2)
Upload_File = driver.find_element_by_xpath('/html/body/label/input').send_keys("YourFilePath")

如果显示错误,请告诉我。

【讨论】:

  • 这个工作完美。谢谢斯瓦鲁普。你能告诉我你从哪里得到这个 XPATH driver.find_element_by_xpath('/html/body/label/input')
  • 实际上,我尝试找到一个<input type="file">,通常在该位置附加/加载文件。我只需右键单击该元素并从开发工具中复制Xpath
猜你喜欢
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 2012-01-29
  • 2017-10-02
  • 2018-05-30
相关资源
最近更新 更多