MerlinB
MerlinB

Reputation: 21

Selecting a submit button with Python (Selenium)

I want to automate a Github repository with Python (Selenium)while I use cmd. I got to the last step: 'Create a new repository' on Github, but can't let python click on "Create repository".

Thanks for every help.

I have tried: searchBar = driver.find_elements_by_css_selector('button.first-in-line').click() and 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>

I expect that python automatically clicks on the "Create repository" submit button, to finish the new git repository.

Upvotes: 2

Views: 5432

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193108

To click() on the element with text as Create repository you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

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

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

Upvotes: 1

KunduK
KunduK

Reputation: 33384

When you use find_elements_by_css_selector it will return a list.Instead of find_elements_by_css_selector you must use find_element_by_css_selector

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

However if you want to use find_elements_by_css_selector then you should use index to get the first match and then click like below code.

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

Upvotes: 1

Kushan Gunasekera
Kushan Gunasekera

Reputation: 8566

Try this,

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

One thing, always try to use driver.find_elements_by_xpath() which help you to minimize lot of errors.

Upvotes: 0

Related Questions