Selenium Webdriver无法找到单击按钮的元素

问题描述 投票:0回答:4

我不知道用于单击按钮的元素。

我试着像这样写:

driver.find_element_by_xpath('//*/input[@type="button"]').click()

错误信息:

raise exception_class(message,screen,stacktrace)selenium.common.exceptions.ElementNotVisibleException:Message:元素不可见

HTML:

<input type="button" name="ctl00$c3$g_6_f947_400a_aa18_59efd84584ae$ctl00$toolBarTbl$RightRptControls$ctl00$ctl00$diidIOSaveItem" value="Save" onclick="if (!PreSaveItem()) return false;if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ctl33$g_69_f947_400a_aa18_59efd84584ae$ctl00$toolBarTbl$RightRptControls$ctl00$ctl00$diidIOSaveItem&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, true))" id="ctl00_ctl33_g_696_f947_400a_aa18_59efd84584ae_ct0_toolBarTbl_RightRptControls_ctl00_ctl00_diidIOSaveItem" accesskey="O" class="ms-ButtonHeightWidth" target="_self">
python selenium xpath css-selectors webdriverwait
4个回答
0
投票

“保存”字样可见吗?如果是这样你可以试试这个:

driver.find_element_by_xpath("//*[contains(text(), 'Save')]").click()

0
投票

你试过寻找价值吗?

driver.find_element_by_xpath('//*/input[@value="Save"]').click()

如果这不起作用,如果您可以上传正在测试的页面的HTML或提供URL,将会很有帮助。


0
投票

不知道为什么你使用//*/input而不是直接使用//input。这是解决方案。

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

0
投票

所需元素是一个动态元素,因此要定位您必须引入WebDriverWait元素才能使元素可单击,您可以使用以下任一解决方案:

  • 使用CSS_SELECTORWebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ms-ButtonHeightWidth[value='Save'][name$='SaveItem']"))).click()
  • 使用XPATHWebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ms-ButtonHeightWidth' and @value='Save'][contains(@name, 'SaveItem')]"))).click()
  • 注意:您必须添加以下导入: from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
© www.soinside.com 2019 - 2024. All rights reserved.