Click on specific Button (Selenium Python) - python

here I have 2 buttons:
Clicked Button:
<button type="button" data-testid="favorite-button" class="styles__button--1wSNn styles__neutral--17MuV styles__elevation--2fhDh styles__elevationMedium--2eus4 styles__circle--3zgIv styles__small--127Kw"><span class="styles__iconAfter--3xNI0"><div class="FavoriteIcon__icon--2fuH8 FavoriteIcon__small--2hXns FavoriteIcon__favorited--zicAG"><img class="styles__image--2CwxX" src="/boom/client/f0605f03fa478593f75f791e8eea8889.svg" data-testid="heartFilled" alt="Favorited"></div></span></button>
Unclicked Button:
<button type="button" data-testid="favorite-button" class="styles__button--1wSNn styles__neutral--17MuV styles__elevation--2fhDh styles__elevationMedium--2eus4 styles__circle--3zgIv styles__small--127Kw"><span class="styles__iconAfter--3xNI0"><div class="FavoriteIcon__icon--2fuH8 FavoriteIcon__small--2hXns"><img class="styles__image--2CwxX" src="/boom/client/fe5b59d42e7d54796992f8f9914d3e45.svg" data-testid="heartOutline" alt="Favorite"></div></span></button>
How can I make it click only on the unclicked buttons?
I've already tried that:
driver.find_element_by_xpath("//input[#type="image"][#src="/boom/client/fe5b59d42e7d54796992f8f9914d3e45.svg"]).click()
But it doesn't seem to work.
Thanks.

To Click unclicked button Use the below Xpath to click.
driver.find_element_by_xpath("//button[.//img[data-testid='heartOutline']]").click()
To avoid synronization Issue Use WebDriverWait() and wait for element_to_be_clickable()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[.//img[data-testid='heartOutline']]"))).click()
you need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Related

Python Selenium check all checkboxes on page

I'm trying to tick all checkboxes on a page. There are about 50 of them and this code seems to work but only ticks what is visible on the screen. It doesn't scroll down the table and tick the remaining boxes.
def select_checkbox():
checkboxes = driver.find_elements_by_css_selector("//input[#type='checkbox']")
for checkbox in checkboxes:
if not checkbox.is_selected():
checkbox.click() # to tick it
I'm thinking It's only finding those on the screen as the rest are still loading. I need to wait until all checkboxes have loaded but I'm not sure about how to go about this. Any help is appreciated.
Below is the table row
<input type="checkbox" value="1" name="admin_contract_form[price_forms_attributes][51][offered]" id="admin_contract_form_price_forms_attributes_51_offered">
You can use explicit waits presence_of_all_elements_located for all the checkboxes.
Also, to scroll driver.execute_script("arguments[0].scrollIntoView(true);", checkbox) this will likely to get the job done.
Code:
wait = WebDriverWait(driver, 30)
def select_checkbox():
checkboxes = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//input[#type='checkbox']")))
for checkbox in checkboxes:
driver.execute_script("arguments[0].scrollIntoView(true);", checkbox)
if not checkbox.is_selected():
checkbox.click() # to tick it
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Select all the desired checkbox inducing WebDriverWait for the visibility_of_all_elements_located() and click() the individual checkboxes inducing WebDriverWait for the element_to_be_clickable() as follows:
def select_checkbox():
checkboxes = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//input[starts-with(#name, 'admin_contract_form') and starts-with(#id, 'admin_contract_form_price_forms_attributes') and not(#disabled)]")))
for checkbox in checkboxes:
if not checkbox.is_selected():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(checkbox)).click()
PS: click() inducing WebDriverWait will scroll the element into the view by default.
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

Selenium Python, i try to close a cookie button, but it ends clicking on the ad

I tried to close a cookie button on whoscored.com, I located the button, however when the program clicks, it seems to click the ad behind the button, and it ends up opening a new page instead of closing the cookie button. Any idea of what I can do?
url='https://www.whoscored.com/Search/?t=Crystal+Palace'
browse=webdriver.Chrome()
browse.get(url)
time.sleep(4)
cacheButton=browse.find_elements_by_xpath('//button')
cacheButton[1].click() #This is the "I ACCEPT" button.
Here is the html for the button:
<button class="qc-cmp-button" onclick="window.__cmpui("setAndSaveAllConsent",!0)"> I accept </button>
Would appreciate if anyone can help me.
Induce WebDriverWait() and wait for element_to_be_clickable() and following xpath.
WebDriverWait(browse,15).until(EC.element_to_be_clickable((By.XPATH,"//button[#class='qc-cmp-button' and contains(.,'I accept')]"))).click()
Add following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
url='https://www.whoscored.com/Search/?t=Crystal+Palace'
browse=webdriver.Chrome()
browse.get(url)
WebDriverWait(browse,15).until(EC.element_to_be_clickable((By.XPATH,"//button[#class='qc-cmp-button' and contains(.,'I accept')]"))).click()
Browser sanpshot:
Update:
cookie=btn=WebDriverWait(browse,15).until(EC.presence_of_element_located((By.XPATH,"//button[#class='qc-cmp-button' and contains(.,'I accept')]")))
browse.execute_script("arguments[0].click();", cookie)

Unable to check the checkbox using selenium

I'm trying to check the checkbox using Selenium. This is the element of the checkbox.
<div class="ams-item-text ng-binding" ng-bind-html="amssh.create_label(item)" ng-click="toggle_check_node(item)" role="button" tabindex="0">all contract signed</div>
I'm directly copying the x-path of this element and wrote the below code:
browser.find_element_by_xpath('//*[#id="advancedcontents"]/div/div/div[2]/div/div[1]/div[1]/div/div/div[2]/div[2]/div/div[2]/div[3]/div[2]').click()
But it won't never click the check box that I wanted, but click the check box way below, which is with this:
<div class="ams-item-text ng-binding" ng-bind-html="amssh.create_label(item)" ng-click="toggle_check_node(item)" role="button" tabindex="0">future</div>
What could be the issues? I try the checkbox element, or the text element (also clickable) but both doesn't work.
To click on dynamic element induce WebDriverWait() and visibility_of_element_located() and following xpath option.
WebDriverWait(browser,10).until(EC.visibility_of_element_located((By.XPATH,"//div[#class='ams-item-text ng-binding' and text()='all contract signed']"))).click()
You need to import following libraries.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Try below code:
wait = WebDriverWait(browser, 20)
wait.until(EC.element_to_be_clickable(By.XPATH, "//div[contains(.,'all contract signed')]")).click()
Note : Add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
"ams-item-text ng-binding"browser.find_element_by_xpath('//*[#id="advancedcontents"]/div/div/div[2]/div/div[1]/div[1]/div/div/div[2]/div[2]/div/div[2]/div[3]/div[2]').click()

How to get html element inside button?

<button type="button" name="abc" id="abc" class="bp" onmouseover="this.className = 'bp bph'" onmouseout="this.className = 'bp'" onclick="oCV_NS_.promptAction('finish')" style="font-family:"Arial";font-size:9pt">
<span tabindex="0">GENERATE REPORT</span>
</button>
I want to click this button and tried few codes but nothing worked
tried:
driver.find_element_by_id("abc").click();
driver.find_element(By.ID, "abc")
element_by_name also tried
Please try below code and also check if button is not part of iframe:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 10)
button= wait.until(EC.visibility_of_all_elements_located((By.ID, "abc")))
button.click()
Try use with the below xpath:
driver.find_element_by_xpath("//button[#id='abc']/span").click()

How to click on a bootstrap button as per the HTML through Selenium and Python

I am trying to click a button on webpage using selenium script but it is giving me following error using this line:
driver.find_element_by_class_name('btn-primary').click()
Error is as follows:
ElementNotInteractableException: Message: Element <button class="btn-primary btn-text sort-filter-clear-button" type="button"> could not be scrolled into view
HTML of the button element:
<button type="submit" class="btn-primary btn-action bookButton" id="bookButton" data-track="FLT.RD.Book.Bottom"><span class="btn-label">Continue Booking</span></button>
Try to wait for element:
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "bookButton")))
button.click()
It will wait at least 10 seconds, until element will be clickable.
Note: you have to add some exports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
EDIT: you can try also js executor like this:
button = driver.find_element_by_id("bookButton")
driver.execute_script("arguments[0].click();", button)
in case your button is inside an iframe/frame, firstly you have to switch to this frame and only then you can interact with this element:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME("frame_name"))))
# do your stuff
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "bookButton")))
button.click()
driver.switch_to.default_content() # switch back to default content
As per the HTML you have shared and as you mentioned of bootstrap button, moving forward as you are trying to invoke click() on the desired element you need to induce WebDriverWait for the element to be clickable 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-primary.btn-action.bookButton#bookButton>span.btn-label"))).click()
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn-primary btn-action bookButton' and #id='bookButton']/span[#class='btn-label'][contains(.,'Continue Booking')]"))).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

Categories