簡體   English   中英

使用Python(Selenium)選擇一個提交按鈕

[英]Selecting a submit button with Python (Selenium)

我想在使用cmd時使用Python(Selenium)自動化Github存儲庫。 我走到了最后一步:在Github上“創建新存儲庫”,但是不能讓python單擊“創建存儲庫”。

多謝您的協助。

我試過了: searchBar = driver.find_elements_by_css_selector('button.first-in-line').click()searchBar = driver.find_elements_by_css_selector('button.first-in-line').submit()


<button type="submit" class="btn btn-primary first-in-line" data-disable-with="Creating repository…">
        Create repository
</button>

我希望python自動單擊“創建存儲庫”提交按鈕,以完成新的git存儲庫。

當您使用find_elements_by_css_selector它將返回的list.Instead find_elements_by_css_selector必須使用find_element_by_css_selector

driver.find_element_by_css_selector('button.first-in-line').click()

但是,如果要使用find_elements_by_css_selector ,則應使用index獲得第一個匹配項,然后單擊如下代碼。

driver.find_elements_by_css_selector('button.first-in-line')[0].click()

嘗試這個,

searchBar = driver.find_elements_by_css_selector('.button.first-in-line').click()

一件事,總是嘗試使用driver.find_elements_by_xpath()來幫助您最大程度地減少錯誤。

要在具有文本的元素上click()作為“ 創建”存儲庫 ,則必須為element_to_be_clickable()引入WebDriverWait ,並且可以使用以下定位策略之一

  • 使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary.first-in-line"))).click() 
  • 使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-primary first-in-line']"))).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