【问题标题】:Selenium webdriver python cannot find buttonSelenium webdriver python找不到按钮
【发布时间】:2017-09-01 09:21:30
【问题描述】:

我尝试在填写表单后找到保存按钮但失败了,我尝试使用 xpath 和类名、css 选择器...但它们都不起作用。奇怪的是,如果我在仅测试按钮时编写了一个不同的测试它可以工作,但在填写表单后它没有(表单本身和按钮在同一个表单标签下)。 这是我的代码:

wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.btn-group.form-buttons > button[name=\"save\"]")))

driver.find_element_by_css_selector("div.btn-group.form-buttons > button[name=\"save\"]").click()

按钮的xpath:/html/body/main/section/fieldset[1]/div[2]/div[2]/form/div[2]/button[1]

知道为什么 selenium 找不到它吗?请给我一个解决方案?

【问题讨论】:

标签: python html css selenium selenium-webdriver


【解决方案1】:

试试下面的代码,看看它是否适合你-

element = driver.find_element_by_css_selector("div.btn-group.form-buttons > button[name=\"save\"]")
driver.execute_script("arguments[0].click();", element)

更新

element = driver.find_element_by_css_selector("button.btn.btn-alert.btn-gradient.btn-save-form:first-child")
driver.execute_script("arguments[0].click();", element)

更新 2

element = driver.find_element_by_css_selector("button[type='submit'][name='save'][data-speech='Create']")
driver.execute_script("arguments[0].click();", element)

【讨论】:

  • 不工作 :( selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: div.btn-group.form-b​​uttons > button[name="save"]
  • 给我看看你的 DOM。我认为 cssselector 不正确。
  • :(( selenium.common.exceptions.NoSuchElementException: 消息:无法定位元素:button.btn.btn-alert.btn-gradient.btn-save-form:first-child
  • 你能给个网址吗?
  • 在尝试找到按钮之前通过添加 driver.switch_to.default_content() 解决了这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多