How to Click the "OK" Button within an Alert using Python + Selenium - python

I want to click the "OK" button in this pop up dialog
I tried:
driver.switchTo().alert().accept();
but it doesn't work

To click on the OK button within the alert you need to induce WebDriverWait for the desired alert_is_present() and you can use the following solution:
WebDriverWait(driver, 10).until(EC.alert_is_present())
driver.switch_to.alert.accept()
Note : You have to add the following imports :
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Reference
You can find a couple of relevant discussions in:
Python click button on alert
How to read the text from the alert box using Python + Selenium
Why switching to alert through selenium is not stable?
Would like to understand why switch_to_alert() is receiving a strikethrough and how to fix

Related

Selenium Unclicable button Python

I have an issue when trying to click this button in Selinium:
The folloying is the html code for each side:
"plus de 26 ans"
"Moins de 26 ans"
I have tride creating a button clicker by id using selenium (for first button):
btn_date = driver.find_element_by_id("radio-ins_age-above_26")
btn_date.click()
I get the error message that it is not clickable.
However, when viewing the html when I switch from one button to other I get the following change:
First button:
There is "::after::" that appears. When I click or press anything else it disappears.
How can I click this button?
Thanks
To click on dynamic element use WebDriverWait() and wait for element to be clickable.
Use the following xpath to click.
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[contains(.,'Plus de 26')]"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//span[contains(.,'Moins de 26')]"))).click()
Import below libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

Can't locate a button using selenium to press on it

I've created a script in python using selenium to click on a like button available in a webpage. I've used xpath in order to locate that button and I think I've used it correctly. However, the script doesn't seem to find that button and as a results it throws TimeoutException error pointing at the very line containing the xpath.
As it is not possible to hit that like button without logging in, I expect the script to get the relevant html connected to that button so that I understand I could locate it correctly.
I've tried with:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = "https://www.instagram.com/p/CBi_eIuAwbG/"
with webdriver.Chrome() as driver:
wait = WebDriverWait(driver,10)
driver.get(link)
item = wait.until(EC.visibility_of_element_located((By.XPATH,"//button[./svg[#aria-label='Like']]")))
print(item.get_attribute("innerHTML"))
How can I locate that like button visible as heart symbol using selenium?
To click on Like Button induce WebDriverWait() and wait for visibility_of_element_located() and below xpath.
Then scroll the element into view and click.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.get("https://www.instagram.com/p/CBi_eIuAwbG/")
element=WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//button[.//*[name()='svg' and #aria-label='Like']]")))
element.location_once_scrolled_into_view
element.click()
You can do it like this
likeSVG = driver.find_element(By.CSS_SELECTOR, 'svg[aria-label="Like"]')
likeBtn = likeSVG.find_element(By.XPATH, './..')
likeBtn.click()
likeBtn is equal to the parent of the likeSVG div as you can use XPATH similar to file navigation commands in a CLI.
Try using the .find_element_by_xpath(xPath) method (Uses full xpath):
likeXPATH = "/html/body/div[1]/section/main/div/div[1]/article/div[2]/section[1]/span[1]/button"
likeElement = driver.find_element_by_xpath(likeXPATH)
likeElement.click()

Python Selenium closing a pop-up window

I'd like to close this window opened up on Amazon web site by using Selenium with Python. I've tried find_element_by_xpath, but it doesn't work. Here's the snippet of the code;
close_to_list = browser.get("/html/body/div[4]/div/div/div[2]/div[2]/div[2]/div[1]/div[2]/div/div/table/tbody/tr[2]")
I get the xpath of that 'X' button but I guess I need to close it as switch_to_alert, but I'm new to this era so I couldn't write it properly.
Here's the image view;
enter image description here
You don't need to use switch_to_alert() as well as get() to close modal window. Just try to close it with below code:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[aria-label="Close"]'))).click()
This allows you to wait for button appearance and click the button

Python Selenium click() method on web application

I am trying to write a program which will interact with a web application.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()
browser.get('https://www.easports.com/fifa/ultimate-team/web-app/')
element = WebDriverWait(browser, 20).until(EC.presence_of_element_located(\
(By.ID, 'Login')))
element.click()
print("Found %s" % element)
print("locatione %s" % element.location)
By design it should've open the browser, wait for page to load and then find and click "Login" button. "Login" button is found, but it doesn't click on it. Location is obviously printing wrong coordinates too. What are possible reasons and solutions for this problem?
Try locating the button with another logic. For example, via class:
element = browser.find_element_by_class_name('call-to-action')
element.click()
To find and click the Login button you can use the following line of code :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='standard call-to-action']"))).click()

Selenium click hidden button

I want to control webpage with selenium and python
Python code;
menu = browser.find_element_by_css_selector(".nav")
hidden = browser.find_element_by_link_text('Soluton')
element = browser.find_element_by_xpath("//div[#class=\"two-columns\"]")
Option 1: ActionChains(browser).move_to_element(menu).click(hidden)
Option 2 : ActionChains(browser).move_to_element(element).click(hidden)
html code;
I want to click "Solution" button under the nav menu.
However, Solution be in under the nav menu. So it's hidden.
so, I type following codes ;
 Option 1:
ActionChains(browser).move_to_element(menu).click(hidden)
 Option 2 :
ActionChains(browser).move_to_element(element).click(hidden)
But selenium not happen anything and not give any error message.
How can I click a button under nav menu with selenium ?
Thanks
If I have understood your Question and your Requirement we need to Mouse Hover over the WebElement with text as Actions where 2 options pops out as Request and Solution and you want to click() on Solution. To achieve that you can use the following code block :
#import
from selenium.webdriver.common.action_chains import ActionChains
#code block
menu = driver.find_element_by_xpath("//a[#class='incident-action']/i[#class='icon-ellipsis-h']")
ActionChains(driver).move_to_element(menu).perform()
driver.find_element_by_xpath("//div[#class='action-list' and #id='header-actions-list']//a[#class='icon-arrow' and contains(text(),'Solution')]").click()
You can try to click Actions to make actions list visible and then click on required Solution option:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
actions = wait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Actions")))
actions.click()
solution = wait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Solution")))
solution.click()

Categories