簡體   English   中英

Selenium Webdriver找不到單擊按鈕的元素

[英]Selenium Webdriver Cannot find the element of the click button

我不知道用來單擊按鈕的元素。

我試着像這樣寫:

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

錯誤信息:

引發exception_class(消息,屏幕,堆棧跟蹤)selenium.common.exceptions.ElementNotVisibleException:消息:元素不可見

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">

“保存”一詞可見嗎? 如果是這樣,您可以嘗試以下操作:

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM