How to click on this button element using Selenium and Python - python

Here is the full 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
driver = webdriver.Edge(executable_path = r"C:\Users\H\Desktop\Automated_Tasks\msedgedriver.exe") # Modify the path here...
# Navigate to URL
driver.get("https://powerusers.microsoft.com/t5/notificationfeed/page")
#accept cookies
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/center/div[1]/div/div[2]/button[1]"))).click()
#click button
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='lia-button lia-button-primary view-more-icon lia-link-ticket-post-action' and #id='viewMoreLink'][contains(#href, 'notificationList')]/span[text()='Show more']"))).click()
The following line:
button=WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "viewMoreLink"))).click()
gives an error:
File C:\Program Files\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py:249 in check_response
raise exception_class(message, screen, stacktrace)
ElementClickInterceptedException: element click intercepted: Element is not clickable at point (476, 1865)
(Session info: MicrosoftEdge=109.0.1518.61)
Here is the HTML for the button I'm trying to click.
<a onclick="return LITHIUM.EarlyEventCapture(this, 'click', true)" class="lia-button lia-button-primary view-more-icon lia-link-ticket-post-action" data-lia-action-token="gY01pJ4IhqNcqA8Ouq1d20HgZFI9CVTHrEYgxObqxWantjAxFsOxTacdu8LdHjd0" rel="nofollow" id="viewMoreLink" href="https://powerusers.microsoft.com/t5/notificationfeed/page.notificationlist.notificationlistitems.viewmorelink:viewmore/notificationfeed.lastLoadedTimestamp/1674422253100/notificationfeed.lastViewedTimestamp/1674505573710/container_element/notificationList"><span>Show more</span></a>
Here is a picture showing where the desired element is in:

As per the given HTML to click on the element with text as Show more you need 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.lia-button.lia-button-primary.view-more-icon.lia-link-ticket-post-action#viewMoreLink[href$='notificationList'] > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='lia-button lia-button-primary view-more-icon lia-link-ticket-post-action' and #id='viewMoreLink'][contains(#href, 'notificationList')]/span[text()='Show more']"))).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 find a relevant detailed discussion in ElementClickInterceptedException: Message: element click intercepted Element is not clickable error clicking a radio button using Selenium and Python

Related

"Accept Cookies" popup with Selenium in Python using explicit wait

I have been trying to webscrape some information from a webside with Selenium in Python. I load the webside, but I can't accept the cookies, which is necessary to continue. I have tried to use explicitly wait as I suspected that the problem was that the webside loaded before the "accept" bottom becomes clickable.
My code is:
import os
import until as until
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 as EC
os.environ['PATH'] += r"C:\Users\BackUp HDL\AppData\Local\Programs"
driver = webdriver.Chrome()
driver.get("https://watchmedier.dk/latest/filtered? sitesFilter=policywatch.dk&sitesFilter=shippingwatch.dk&sitesFilter=mobilitywatch.dk&sitesFilter=energiwatch.dk&sitesFilter=finanswatch.dk&sitesFilter=ejendomswatch.dk&sitesFilter=mediawatch.dk&sitesFilter=agriwatch.dk&sitesFilter=fodevarewatch.dk&sitesFilter=medwatch.dk&sitesFilter=kapwatch.dk&sitesFilter=itwatch.dk&sitesFilter=ctwatch.dk&sitesFilter=watchmedier.dk&sitesFilter=advokatwatch.dk")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="notice"]/div[3]/button[2]')))
driver.find_element_by_xpath('//*[#id="notice"]/div[3]/button[2]').click()
Whenever I try to run the code I get an error. Can someone please help me out?
The element Accepter is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://watchmedier.dk/latest/filtered? sitesFilter=policywatch.dk&sitesFilter=shippingwatch.dk&sitesFilter=mobilitywatch.dk&sitesFilter=energiwatch.dk&sitesFilter=finanswatch.dk&sitesFilter=ejendomswatch.dk&sitesFilter=mediawatch.dk&sitesFilter=agriwatch.dk&sitesFilter=fodevarewatch.dk&sitesFilter=medwatch.dk&sitesFilter=kapwatch.dk&sitesFilter=itwatch.dk&sitesFilter=ctwatch.dk&sitesFilter=watchmedier.dk&sitesFilter=advokatwatch.dk")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Accepter']"))).click()
Using XPATH:
driver.get("https://watchmedier.dk/latest/filtered? sitesFilter=policywatch.dk&sitesFilter=shippingwatch.dk&sitesFilter=mobilitywatch.dk&sitesFilter=energiwatch.dk&sitesFilter=finanswatch.dk&sitesFilter=ejendomswatch.dk&sitesFilter=mediawatch.dk&sitesFilter=agriwatch.dk&sitesFilter=fodevarewatch.dk&sitesFilter=medwatch.dk&sitesFilter=kapwatch.dk&sitesFilter=itwatch.dk&sitesFilter=ctwatch.dk&sitesFilter=watchmedier.dk&sitesFilter=advokatwatch.dk")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='SP Consent Message']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#title='Accepter']"))).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
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

How to click on the Create Account button within https://mail.protonmail.com/ signup page using Selenium and Python

Everything is working except the button. The submit button is not being clicked. Can anyone help me out? I believe it may have to do with the fact that I changed the frame to "top". But, I am unsure how to change it back.
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 as EC
import time
url = 'https://protonmail.com/'
driver = webdriver.Chrome('/Users/edenhikri/Desktop/chromedriver')
driver.get(url)
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.btn.btn-default.btn-short'))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'.panel-heading'))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#freePlan'))).click()
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,'password'))).send_keys('test123')
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,'passwordc'))).send_keys('test123')
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,".top")))
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,'username'))).send_keys('myUsername')
driver.find_element_by_class_name('btn.btn-submit').click()
error:
no such element: Unable to locate element: {"method":"class name","selector":"btn.btn-submit"}
button code:
<button type="submit" class="btn btn-submit" name="submitBtn">Create Account</button>
I have also tried using xpath but I get the same error that it cannot find this:
driver.find_element_by_xpath("//button[#class=“btn btn-submit”]").click()
Please use this xpath //*[contains(text(),"Create Account")]
The CREATE ACCOUNT button is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following css-selectors based Locator Strategies:
driver.get("https://protonmail.com/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-default.btn-short[href='signup']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[data-plan='free']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#freePlan"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.top[title='Registration form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#username"))).send_keys("Eden")
driver.switch_to.default_content()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#password"))).send_keys("Eden")
driver.find_element_by_css_selector("input#passwordc").send_keys("Eden")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.bottom[title='Registration form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-submit"))).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
Browser Snapshot:

How to click on the Copy button within https://www.deepl.com/translator using Selenium and Python

i am trying to automate some translation through selenium using this url https://www.deepl.com/translator . However I am unable to click on copy button shown in photo here.red marking on button . Inspecting this reveals this html code
<button tabindex="130" _dl-connected="1" _dl-attr="onClick: $0.doCopy" _dl-attr-type="null">
<svg width="20" height="22" viewBox="0 0 20 22" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M16.2949 15.7893H8.59364C7.36747 15.7893 6.37793 14.7947 6.37793 13.5623V3.22705C6.37793 1.9946 7.36747 1 8.59364 1H16.3164C17.5425 1 18.5321 1.9946 18.5321 3.22705V13.5839C18.5106 14.7947 17.521 15.7893 16.2949 15.7893Z" stroke-miterlimit="10"></path>
<path d="M11.1966 20.9997H3.34478C2.05408 20.9997 1 19.9402 1 18.6429V7.35629C1 6.05898 2.05408 4.99951 3.34478 4.99951H11.1966C12.4873 4.99951 13.5414 6.05898 13.5414 7.35629V18.6645C13.5414 19.9402 12.4873 20.9997 11.1966 20.9997Z" fill="white" stroke-miterlimit="10"></path>
</svg>
</button>
Please guide on how to locate this button using both xpath(what should be tag and attribute to be used here) and one other method say css locator. I would be obliged.
The code I used to try and locate the button is:
cpy_btn = driver.find_elements_by_xpath('//*[#id="dl_translator"]/div[1]/div[4]/div[3]/div[4]/div[1]/button')
And later I used
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//button[#tabindex='130'])")))
but both didn't work.
The error message I received is: selenium.common.exceptions.ElementClickInterceptedException: Message: Element <button> is not clickable at point (1177,601) because another element <p> obscures it
The desired element is a dynamic element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://www.deepl.com/translator')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.lmt__target_toolbar__copy > button"))).click()
Using XPATH:
driver.get('https://www.deepl.com/translator')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='lmt__target_toolbar__copy']/button"))).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
Browser Snapshot:
You can try 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
Then use :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//textarea)[2]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='lmt__target_toolbar__copy']/button"))).click()
Whith ActionChains :
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='lmt__target_toolbar__copy']/button")))).click().perform()

Python Selenium: Element Is Not Clickable At Point

I am using the code below to click on the highlighted tab that you see on the attached screenshot with the corresponding HTML code. The tab is always visible on the bottom of the script so there is no need to scroll down to it.
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "tab-button-settings")))
element.click()
The error that I get is below. What am I doing wrong?
Message: unknown error: Element <a class="tab-button has-icon icon-only" href="#" role="tab" ng-
reflect-tab="[object Object]" id="tab-button-settings" aria-controls="tabpanel-t0-1" aria-
selected="false">...</a> is not clickable at point (1440, 1017). Other element would receive the
click: <div class="click-block click-block-enabled click-block-active"></div>
(Session info: chrome=78.0.3904.108)
you can use action class to avoid above issue
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "tab-button-settings"))
ActionChains(driver).move_to_element(element).click().perform()
Another way alternative to javascript is to use touch actions
from selenium.webdriver.common.touch_actions import TouchActions
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "tab-button-settings")))
touchactions = TouchActions(driver)
touchactions.tap(element).perform()
The desired element is an Angular element so to locate and click() on the element 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.tab-button.has-icon.icon-only#tab-button-settings"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='tab-button has-icon icon-only' and #id='tab-button-settings']"))).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
In case the unknown error still occurs you may have to induce WebDriverWait additionally for the invisibility_of_element_located() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "div.click-block.click-block-enabled.click-block-active")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.tab-button.has-icon.icon-only#tab-button-settings"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.XPATH, "//div[#class='click-block click-block-enabled click-block-active']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='tab-button has-icon icon-only' and #id='tab-button-settings']"))).click()
References
You can find a couple of relevant discussions in:
Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click
Element MyElement is not clickable at point (x, y)… Other element would receive the click
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python

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