简体   繁体   English

Selenium Webdriver找不到单击按钮的元素

[英]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(&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">

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.

相关问题 Selenium Webdriver无法找到输入框的元素 - Selenium Webdriver Cannot find the element of the Input box Selenium Webdriver Python找不到按钮 - Selenium webdriver python cannot find button 按 find_element_by_class_name 单击按钮不起作用 python selenium webdriver 不起作用 - Click button by find_element_by_class_name not working python selenium webdriver NOT working 如何在Selenium Webdriver python3.7中使用带有内部html的find_element_by_xpath单击按钮 - how to click button using find_element_by_xpath with inner html in selenium webdriver python3.7 Selenium WebDriver即使看起来似乎也无法单击元素 - Selenium WebDriver cannot click element even though it seems to be visible 带有 Python 的 Selenium Webdriver:元素不可点击且无法读取 null 的属性“点击” - Selenium Webdriver with Python: Element is not clickable & Cannot read property 'click' of null 使用 Python 和 selenium webdriver 单击没有 ID/名称的按钮元素 - Click on button element without id/name using Python and selenium webdriver 无法点击“保存”按钮(Selenium WebDriver-Python-Chrome) - Cannot click “Save” button (selenium webdriver - python - chrome) 如何找到“收藏夹”按钮并使用Selenium WebDriver单击它? - How can find the Favorite button and click it with Selenium WebDriver? 如何在 Python selenium 中找到要单击按钮的元素? - How to find element to click button in Python selenium?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM