我不知道用于单击按钮的元素。
我试着像这样写:
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("ctl00$ctl33$g_69_f947_400a_aa18_59efd84584ae$ctl00$toolBarTbl$RightRptControls$ctl00$ctl00$diidIOSaveItem", "", true, "", "", false, true))" id="ctl00_ctl33_g_696_f947_400a_aa18_59efd84584ae_ct0_toolBarTbl_RightRptControls_ctl00_ctl00_diidIOSaveItem" accesskey="O" class="ms-ButtonHeightWidth" target="_self">
“保存”字样可见吗?如果是这样你可以试试这个:
driver.find_element_by_xpath("//*[contains(text(), 'Save')]").click()
你试过寻找价值吗?
driver.find_element_by_xpath('//*/input[@value="Save"]').click()
如果这不起作用,如果您可以上传正在测试的页面的HTML或提供URL,将会很有帮助。
不知道为什么你使用//*/input
而不是直接使用//input
。这是解决方案。
driver.find_element_by_xpath("//input[@type='button' and @value='Save']").click()
所需元素是一个动态元素,因此要定位您必须引入WebDriverWait元素才能使元素可单击,您可以使用以下任一解决方案:
CSS_SELECTOR
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ms-ButtonHeightWidth[value='Save'][name$='SaveItem']"))).click()
XPATH
:
WebDriverWait(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