[英]Selenium Webdriver Cannot find the element of the click button
I don't know the element used to click the button. 我不知道用来单击按钮的元素。
I tried to write like this: 我试着像这样写:
driver.find_element_by_xpath('//*/input[@type="button"]').click()
Error message: 错误信息:
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementNotVisibleException: Message: element not visible 引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见
HTML: 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">
Is the 'Save' word visible? “保存”一词可见吗? if so you can try this: 如果是这样,您可以尝试以下操作:
driver.find_element_by_xpath("//*[contains(text(), 'Save')]").click()
Have you tried looking for the value? 您是否尝试过寻找价值?
driver.find_element_by_xpath('//*/input[@value="Save"]').click()
If this doesn't work it would be helpful if you could upload the HTML for the page you are testing or provide the URL. 如果这不起作用,则可以上载要测试的页面的HTML或提供URL,这将很有帮助。
Not sure why you are using //*/input
rather using direct //input
. 不知道为什么使用//*/input
而不是直接//input
。 Here is the solution. 这是解决方案。
driver.find_element_by_xpath("//input[@type='button' and @value='Save']").click()
The desired element is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions: 所需元素是动态元素,因此要定位该元素,必须使WebDriverWait 变为可单击元素,并且可以使用以下任一解决方案:
Using CSS_SELECTOR
: 使用CSS_SELECTOR
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ms-ButtonHeightWidth[value='Save'][name$='SaveItem']"))).click()
Using XPATH
: 使用XPATH
:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ms-ButtonHeightWidth' and @value='Save'][contains(@name, 'SaveItem')]"))).click()
Note : You have to add the following imports : 注意 :您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.