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.

2
  • What error or result do you get when you try these things? Commented Jun 15, 2019 at 4:54
  • use the GitHub API Commented Jun 15, 2019 at 12:29

3 Answers 3

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()
Sign up to request clarification or add additional context in comments.

Comments

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
    

1 Comment

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

2 Comments

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.
2. The advise to always use xpath is at least debatable if not a bad advise, especially when class names are concerned. See apress.com/in/blog/all-blog-posts/… for more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.