(Python)通过“浏览”窗口Selenium上载文件

2024-10-03 23:28:40 发布

您现在位置:Python中文网/ 问答频道 /正文

为了测试,我需要

  1. 通过浏览窗口选择文件(See screenshot here
  2. 单击“打开”(将文件上载到网站),然后
  3. 点击“上传”。在

我如何通过硒来做到这一点?”原因如下:

# hident2 is the name of "Choose File" element
wd.find_element(By.XPATH("//input[@id='hident2']")).sendKeys("C:\\Users\\file-to-upload.xml"); 

# input.btn.primary is the name of "Upload" button element**
wd.find_element_by_css_selector("input.btn.primary").click()

我得到以下错误:

TypeError: 'str' object is not callable

我做错什么了?在


Tags: 文件ofthenameinputhereiselement
2条回答

您没有正确使用find_element()方法。将By.XPATH作为单独的参数传递:

wd.find_element(By.XPATH, "//input[@id='hident2']")

或者,使用直接快捷方式:

^{pr2}$

或者,只需使用“按id”内置定位器:

wd.find_element_by_id("hident2")

另外,该方法称为send_keys(),而不是{}。在

相关问题 更多 >