2

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
MerlinB
  • 21
  • 1
  • 3

3 Answers3

1

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()
KunduK
  • 32,888
  • 5
  • 17
  • 41
1

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • From what I saw, GitHub website is mostly synchronous, and I don't find any clue in the question that implies that the problem is related to timings (though I admit that I haven't tried it myself). How did you get to that conclusion? I know that timing is a pretty common problem but often people assume that a problem is related to timing when it's not, and just introduce unnecessary complexity into the test code which also often hides the real problems. – Arnon Axelrod Jun 15 '19 at 05:05
0

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.

Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
  • 1. A dot in a css selector signifies a class name. As the element mentioned in the question doesn't have a "button" class, your suggestion isn't going to work. – Arnon Axelrod Jun 15 '19 at 04:45
  • 2. The advise to always use xpath is at least debatable if not a bad advise, especially when class names are concerned. See https://www.apress.com/in/blog/all-blog-posts/best-practices-for-choosing-locators-for-selenium/16169400 for more details – Arnon Axelrod Jun 15 '19 at 04:49