Selecting a submit button with Python (Selenium) - python

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.

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()

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

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.

Related

Selenium findElement(By.CSS_SELECTOR)

I have this button on the page:
<button type="submit" class="sc-pjTqr dzmlqP">Continue</button>`
I checked the documentation and StackOverflow answers and it seems to me that the solution should be:
continue = driver.find_element(By.CSS_SELECTOR,"button.sc-pjTqr.dzmlqP")
But does not work.
I searched for solutions, but I didn't understand. Why?
The classnames i.e. sc-pjTqr, dzmlqP are dynamically generated and is bound to chage sooner/later. They may change next time you access the application afresh or even while next application startup. So can't be used in locators.
Solution
To identify the element with text as Continue you can use the following locator strategy:
Using xpath:
continue = driver.find_element(By.XPATH, "//button[text()='Continue']")
Ideally to identify the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:
Using XPATH:
continue = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Continue']")))
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
The OP doesn't confirm the url where this button lives, so I can test the solution:
button = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "sc-pjTqr")))
There are other ways as well.

Cannot click tag <a> with <i class> selenium python

I want to make a simple attendance automation with python for my college, I make this for ease, because this took so many button to click but, I cannot click the tag <a> with tag <i> inside it.
<a href="http://siakad.polinema.ac.id/mahasiswa/tr_absensi/add"
class="btn btn-sm green-meadow btn-add-data" id="btn-add-wizard">
<i class="fa fa-plus"></i> Absen</a>
I use this wait function, and still not directed into that link. It shows nothing on result. Process finished with exit code 0
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(#href, ""'http://siakad.polinema.ac.id/mahasiswa/tr_absensi/add')]"))).click()
I tried using ID and LINK_TEXT and still showing Process finished with exit code 0.
Thank you, apologize for my english. Enghlish is not my main language. If you need more information about my question, please let me know.
To click on the element with text as Absen 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, "a.btn.btn-sm.green-meadow.btn-add-data#btn-add-wizard[href$='id/mahasiswa/tr_absensi/add']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn-sm green-meadow btn-add-data' and #id='btn-add-wizard'][contains(#href, 'id/mahasiswa/tr_absensi/add')]"))).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

Unable to send date as text to datepicker field using send_keys when max attribute is set using ChromeDriver and Selenium

I am trying to use chromedriver to download some files.
I have switched to chromedriver because in firefox the link I need to click opens a new window and the download dialog box appears even after all the required settings and I wasn't able to get around it.
chromedriver works fine for the download but I can't seem to send_keys() to the element below, it works on firefox but can't seem to get it to work on this.
<input name="" value="" id="was-returns-reconciliation-report-start-date" type="date" class="was-form-control was-input-date" data-defaultdate="" data-mindate="" data-maxdate="today" data-placeholder="Start Date" max="2020-02-12">
I have tried:
el = driver.find_element_by_id("was-returns-reconciliation-report-start-date")
el.clear()
el.send_keys("2020-02-01")
el.send_keys(Keys.ENTER) # Separately
# Tried without clear as well
# no error but the date didn't change in the browser
driver.execute_script("document.getElementById('was-returns-reconciliation-report-start-date').value = '2020-01-05'")
# No error and no change in the page
To send a character sequence to an <input> field ideally you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using ID:
el = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.ID, "was-returns-reconciliation-report-start-date")))
el.clear()
el.send_keys("2020-02-12")
Using CSS_SELECTOR:
el = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "input.was-form-control.was-input-date#was-returns-reconciliation-report-start-date")))
el.clear()
el.send_keys("2020-02-12")
Using XPATH:
el = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//input[#class='was-form-control was-input-date' and #id='was-returns-reconciliation-report-start-date']")))
el.clear()
el.send_keys("2020-02-12")
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

Python Selenium: Having trouble fixing NoSuchElementException for a specific button

I'm trying to press a button with selenium in python. I am not able to locate it with xpath or css selector.
For the rest of the things in the script I have been using xpath and it works fine, but this button's xpath seems to be relative each time I open it. Therefore I have tried to access is with the css selector.
The element I am trying to access looks like this:
<button type="button" data-dismiss="modal" class="btn btn-primary pull-right">Opret AnnonceAgent</button>
The css selector from inspect in chrome:
#\35 d167939-adc2-0901-34ea-406e6c26e1ab > div.modal-footer > div > button.btn.btn-primary.pull-right
Let me know if I should post more html.
I have tried many stack oveflow questions, and tried many variatiosn of the css selector, like putting it as:
driver.find_element_by_class_name('#\35 d167939-adc2-0901-34ea-406e6c26e1ab.div.modal-footer.div.button.btn.btn-primary.pull-rightdiv.button.btn.btn-primary-pull-right').click()
driver.find_element_by_class_name('div.button.btn.btn-primary-pull-right').click()
driver.find_element_by_class_name('button.btn.btn-primary-pull-right').click()
driver.find_element_by_class_name('btn.btn-primary-pull-right').click()
driver.find_element_by_class_name('btn-primary-pull-right').click()
I have also tried a sleep timer.
The button is in a window that opens when you press the previous button, with the background greyed out, if that helps. Picture.
# This opens up the window in which to press the next button (works fine)
button = driver.find_element_by_xpath('//*[#id="content"]/div[1]/section/div[2]/button')
button.click()
driver.implicitly_wait(15)
time.sleep(5)
# This is what doesn't work
driver.find_element_by_class_name('button.btn.btn-primary-pull-right').click()
I expect for the program to press the button and it doesn't, it just sits there.
The desired element is within a Modal Dialog Box, so you need to induce WebDriverWait for the desired 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.pull-right[data-dismiss='modal']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-primary pull-right' and #data-dismiss='modal'][text()='Opret AnnonceAgent']"))).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
class_name recives one class as parameter, but you are using css syntax as locator.
With css_selector
driver.find_element_by_css_selector('button.btn.btn-primary-pull-right')
Which means <button> tag with 2 classes, btn and btn-primary-pull-right
With class_name
driver.find_element_by_class_name('btn-primary-pull-right')
My guess was right.There was an iframe which stopping to access the element.You have to switch to iframe first to access the button element.Try now with following code.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
#WebDriverWait(driver,20).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[starts-with(#id,"rdr_")]')))
WebDriverWait(driver,20).until(expected_conditions.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[#id="qualaroo_dnt_frame"]')))
driver.find_element_by_css_selector('button.btn.btn-primary').click()

How to click on angular dropdown menu in Selenium

I am trying to use python selenium to automate some report generating on a website, however I am struggling to identify the element I need to click on the page due to the javascript. In firefox there is a DOM Event icon when I inspect the element. I have tried alot of variations including xpath etc but no luck.
<account-groups ng-if="EventsList.ToggleService.accountGroup();">
<button class="btn-default" ng-disabled="AccountGroupsCtrl.isDisabled()" ng-click="AccountGroupsCtrl.toggleFlyout()">
</button>
</account-groups>
EDIT For anyone viewing this the issue was because I had not switched frames using driver.switch_to.frame('frame_name'). Once this step is carried out the rest of the solutions below worked in identifying the elements. Thanks
As per the HTML you have shared it is not clear if the WebElement is a Dropdown. However as the desired element is an Angular element to invoke click() on the element you need to induce WebDriverWait and you can use either of the following solutions:
CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn-default[ng-click^='AccountGroupsCtrl']"))).click()
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn-default' and starts-with(#ng-click,'AccountGroupsCtrl')]"))).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
You can use following code for selecting from dropdown.
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id('*****'))
select.select_by_visible_text('****')

Categories