简体   繁体   English

无法点击“保存”按钮(Selenium WebDriver-Python-Chrome)

[英]Cannot click “Save” button (selenium webdriver - python - chrome)

I am fairly new to Python and started learning. 我刚接触Python并开始学习。 I am trying to automate data entry. 我正在尝试自动化数据输入。 I am stuck at the "save" button. 我被卡在“保存”按钮上。 How do I find the right information and click it to save? 如何找到正确的信息并单击以保存?

Thank you so much 非常感谢

PyGuy 皮吉


Element 元件

<input type="submit" value="Save">

Xpath Xpath

//*[@id="decorated-admin-content"]/div/div/form/div[10]/div/input

Selector 选择器

#decorated-admin-content > div > div > form > div.buttons-container > div > input[type="submit"]

On my python script, I have entered 在我的python脚本上,我已经输入

from selenium import webdriver
from selenium.webdriver.common.by import By

driver.findElement(By.xpath("//input[@type='submit'and @value='save']")).click()
# I also tried below
# driver.findElement(By.xpath("//input[@type='submit'][@value='Save']")).click();
# driver.findElement(By.xpath("//*[@id="decorated-admin-content"]"))

If you're using python, the syntax is not right. 如果您使用的是python,则语法不正确。 Python uses snake_case and By uses CONSTANT convention Python使用snake_case和By使用CONSTANT约定

from selenium import webdriver
from selenium.webdriver.common.by import By

driver.find_element(By.XPATH, "//input[@type='submit' and @value='save']").click()

It's actually suggested to use the individual methods for each By if you don't need to be dynamic: 实际上,如果不需要动态,则建议对每个By使用单独的方法:

driver.find_element_by_xpath("//input[@type='submit' and @value='save']").click()

Or css: 或CSS:

driver.find_element_by_css_selector('input[type="submit"]').click()

If that doesn't work, can you post the error traceback you are getting? 如果这不起作用,您可以发布所得到的错误回溯吗?

Did you try with other parameter than xpath ? 您是否尝试使用xpath以外的其他参数? I also had some difficulties with selenium, you can try the following line : 我在硒方面也遇到了一些困难,可以尝试以下方法:

driver.findElement(By.tagName("form")).submit()

It's works for me and is useful to validate forms 它对我有用,对验证表单很有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM