Element that could be previously clicked is now no longer clickable? [duplicate] - python

I have a page with source code like the code below. In it after I take an action an “Undo” and a “Close” button appear. I’m trying to click the “Close” button. I’ve tried all three of the chunks of code below, none are working. Can someone please point out what I’m doing wrong, or suggest something else to try?
html source:
<div class="_3Aslx7L3GVI4XM7PUyYKza action-bar"><div class="container"><i class="success-icon fontello-ok-circle"></i><div class="success-message">Your stuff is going to <span>place</span> is on its way.</div><div class="gh69ID1m3_xtdTUQuwadU"><button class="c-button c-button--gray"> Undo</button></div><div class="gh69ID1m3_xtdTUQuwadU"><button class="c-button c-button--blue"> Close</button></div></div></div>
code attempts:
#driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()
driver.find_element_by_css_selector('.c-button.c-button--blue').click()
#driver.find_element_by_link_text('Close').click()
error:
---------------------------------------------------------------------------
ElementNotVisibleException Traceback (most recent call last)
<ipython-input-15-6d570be770d7> in <module>()
1 #driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()
----> 2 driver.find_element_by_css_selector('.c-button.c-button--blue').click()
3 #driver.find_element_by_link_text('Close').click()
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in click(self)
78 def click(self):
79 """Clicks the element."""
---> 80 self._execute(Command.CLICK_ELEMENT)
81
82 def submit(self):
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
626 params = {}
627 params['id'] = self._id
--> 628 return self._parent.execute(command, params)
629
630 def find_element(self, by=By.ID, value=None):
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
318 response = self.command_executor.execute(driver_command, params)
319 if response:
--> 320 self.error_handler.check_response(response)
321 response['value'] = self._unwrap_value(
322 response.get('value', None))
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
ElementNotVisibleException: Message: element not interactable
(Session info: chrome=72.0.3626.109)
(Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)

The element with text as Close is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.action-bar button.c-button.c-button--blue"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class, 'action-bar')]//button[#class='c-button c-button--blue' and normalize-space()='Close']"))).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 using XPATHs and Action class for this kind of scenarios.
BTN_xpath = //*[contains(#class, 'c-button--blue')]
WebElement btn = driver.find_element_by_xpath(BTN_xpath);
Actions ac = new Actions(driver);
ac.click(btn).build().perform();
Also, if you are using the Firefox browser, there could be issues where its not able to interact with elements that were not visible before in the page. So, try checking a with a different browser as well.

Related

Can't find button by classname selenium

I am trying to login to https://ok.ru/
I can't find button by css selector or classname .
I want to click the button
My code returns error
Message: no such element: Unable to locate element: {"method":"css selector","selector":".button-pro __wide"}
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path='chromedriver')
def login(email, password):
driver.get(BASE_URL)
driver.find_element_by_id('field_email').send_keys(email)
driver.find_element_by_id('field_password').send_keys(password)
driver.find_element_by_class_name('button-pro __wide').click()
login(email, password)
Debugging details
<ipython-input-19-30d48e0af276> in login(email, password)
6 driver.find_element_by_id('field_email').send_keys(email)
7 driver.find_element_by_id('field_password').send_keys(password)
----> 8 driver.find_element_by_css_selector('button-pro __wide.button-pro __wide').click()
9
10 login(email, password)
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in find_element_by_css_selector(self, css_selector)
596 element = driver.find_element_by_css_selector('#foo')
597 """
--> 598 return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
599
600 def find_elements_by_css_selector(self, css_selector):
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in find_element(self, by, value)
976 return self.execute(Command.FIND_ELEMENT, {
977 'using': by,
--> 978 'value': value})['value']
979
980 def find_elements(self, by=By.ID, value=None):
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button-pro __wide.button-pro __wide"}
(Session info: chrome=85.0.4183.102)
P.S
I know that I can use xpath, but I need reliable method that uses same classname or id etc

Selenium insert number into table [duplicate]

I have a page with source code like the code below. In it after I take an action an “Undo” and a “Close” button appear. I’m trying to click the “Close” button. I’ve tried all three of the chunks of code below, none are working. Can someone please point out what I’m doing wrong, or suggest something else to try?
html source:
<div class="_3Aslx7L3GVI4XM7PUyYKza action-bar"><div class="container"><i class="success-icon fontello-ok-circle"></i><div class="success-message">Your stuff is going to <span>place</span> is on its way.</div><div class="gh69ID1m3_xtdTUQuwadU"><button class="c-button c-button--gray"> Undo</button></div><div class="gh69ID1m3_xtdTUQuwadU"><button class="c-button c-button--blue"> Close</button></div></div></div>
code attempts:
#driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()
driver.find_element_by_css_selector('.c-button.c-button--blue').click()
#driver.find_element_by_link_text('Close').click()
error:
---------------------------------------------------------------------------
ElementNotVisibleException Traceback (most recent call last)
<ipython-input-15-6d570be770d7> in <module>()
1 #driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()
----> 2 driver.find_element_by_css_selector('.c-button.c-button--blue').click()
3 #driver.find_element_by_link_text('Close').click()
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in click(self)
78 def click(self):
79 """Clicks the element."""
---> 80 self._execute(Command.CLICK_ELEMENT)
81
82 def submit(self):
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
626 params = {}
627 params['id'] = self._id
--> 628 return self._parent.execute(command, params)
629
630 def find_element(self, by=By.ID, value=None):
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
318 response = self.command_executor.execute(driver_command, params)
319 if response:
--> 320 self.error_handler.check_response(response)
321 response['value'] = self._unwrap_value(
322 response.get('value', None))
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
ElementNotVisibleException: Message: element not interactable
(Session info: chrome=72.0.3626.109)
(Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)
The element with text as Close is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.action-bar button.c-button.c-button--blue"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class, 'action-bar')]//button[#class='c-button c-button--blue' and normalize-space()='Close']"))).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 using XPATHs and Action class for this kind of scenarios.
BTN_xpath = //*[contains(#class, 'c-button--blue')]
WebElement btn = driver.find_element_by_xpath(BTN_xpath);
Actions ac = new Actions(driver);
ac.click(btn).build().perform();
Also, if you are using the Firefox browser, there could be issues where its not able to interact with elements that were not visible before in the page. So, try checking a with a different browser as well.

How do I get Selenium to login to the website Costco.com

Picture of the screen it stays on forever:
I am having trouble getting Selenium to login to Costco.com. It basically freezes at the login screen and won't proceed to the next screen.
from selenium import webdriver;
from selenium.webdriver.support.ui import Select;
from selenium.webdriver.common.keys import Keys;
from selenium.webdriver.common.by import By;
import time;
browser = webdriver.Chrome()
browser.get('https://www.costcobusinessdelivery.com/LogonForm?URL=%2f')
email = browser.find_element_by_css_selector('#logonId')
email.click()
email.send_keys('my_email')
password = browser.find_element_by_css_selector('#logonPassword_id')
password.click()
password.send_keys('my_password')
zipcode = browser.find_element_by_css_selector('#deliveryZipCode')
zipcode.click()
zipcode.send_keys('my_zipcode')
login = browser.find_element_by_css_selector('#sign_in_button')
login.click()
After a few minutes of Selenium sitting there it then pops out the traceback. I've tried using beautifulsoup4 to login and pass the data back to Selenium but I 'm not sure if this works. So I first have to navigate to the page I need in Selenium then parse the data with BS4.
TimeoutException Traceback (most recent call last)
<ipython-input-80-ddf2d5259794> in <module>
1 login = browser.find_element_by_css_selector('#sign_in_button')
----> 2 login.click()
C:\ProgramData\Anaconda\lib\site-packages\selenium\webdriver\remote\webelement.py in click(self)
78 def click(self):
79 """Clicks the element."""
---> 80 self._execute(Command.CLICK_ELEMENT)
81
82 def submit(self):
C:\ProgramData\Anaconda\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
631 params = {}
632 params['id'] = self._id
--> 633 return self._parent.execute(command, params)
634
635 def find_element(self, by=By.ID, value=None):
C:\ProgramData\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
C:\ProgramData\Anaconda\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
TimeoutException: Message: timeout
(Session info: chrome=77.0.3865.75)
If the ID is available please use the ID and try out below example.
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
usernameStr = 'putYourUsernameHere'
passwordStr = 'putYourPasswordHere'
zipStr ='putZipCode'
browser = webdriver.Chrome()
browser.get(('https://www.costcobusinessdelivery.com/LogonForm?URL=%2f'))
username = browser.find_element_by_id('logonId')
username.send_keys(usernameStr)
password= browser.find_element_by_id('logonPassword_id')
password.send_keys(passwordStr)
zip= browser.find_element_by_id('logonPassword_id')
zip.send_keys(zipStr)
signInButton = browser.find_element_by_id('sign_in_button')
signInButton.click()
To login in to Costco.com through the url https://www.costcobusinessdelivery.com/LogonForm?URL=%2f you need to to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
css_selector:
driver.get("https://www.costcobusinessdelivery.com/LogonForm?URL=%2f")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#logonId"))).send_keys("est3rz#stackoverflow.com")
driver.find_element_by_css_selector("input#logonPassword_id").send_keys("my_password")
driver.find_element_by_css_selector("input#deliveryZipCode").send_keys("54321")
driver.find_element_by_css_selector("input#sign_in_button").click()
xpath:
driver.get("https://www.costcobusinessdelivery.com/LogonForm?URL=%2f")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='logonId']"))).send_keys("est3rz#stackoverflow.com")
driver.find_element_by_xpath("//input[#id='logonPassword_id']").send_keys("my_password")
driver.find_element_by_xpath("//input[#id='deliveryZipCode']").send_keys("54321")
driver.find_element_by_xpath("//input[#id='sign_in_button']").click()
Browser Snapshot:

Cannot Click the button by using selenium

I try to click the hidden button by scrolling to the element but still not working. Only if i scroll it manually, the coding is working. May I know the reasons? Thank you!
website: https://www1.hkexnews.hk/search/titlesearch.xhtml?lang=en
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
driver = webdriver.Chrome("C:/Users/Ivan.Chak/Desktop/cbbc/chromedriver", chrome_options=options)
driver.implicitly_wait(30)
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml?lang=en")
driver.find_element_by_css_selector("#tier1-select .combobox-input-wrap").click()
driver.find_element_by_css_selector(".searchType .droplist-item:nth-child(2) > a").click()
driver.find_element_by_css_selector("#rbAfter2006 .combobox-field").click()
element = driver.find_element_by_xpath("//a[contains(text(),'Debt and Structured Products')]")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
time.sleep(1)
element.click() <--- This is not working!!!
---------------------------------------------------------------------------
ElementNotInteractableException Traceback (most recent call last)
<ipython-input-323-cf25f85114ab> in <module>()
----> 1 element.click()
C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in click(self)
78 def click(self):
79 """Clicks the element."""
---> 80 self._execute(Command.CLICK_ELEMENT)
81
82 def submit(self):
C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
631 params = {}
632 params['id'] = self._id
--> 633 return self._parent.execute(command, params)
634
635 def find_element(self, by=By.ID, value=None):
C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
ElementNotInteractableException: Message: element not interactable
(Session info: chrome=75.0.3770.142)
It might be possible you are scrolling to the element(button) . Which again is getting hidden if your webpage have any kind of ribbon. Try scrolling to any other element above it. This had solved my problem.
You have to scroll to the each menu item before click, for that you can use JavaScript arguments[0].scrollIntoView(true);
You can use general method to scroll before click like in code below:
def click(element):
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
driver = webdriver.Chrome()
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml?lang=en")
driver.find_element_by_css_selector(".searchType").click()
driver.find_element_by_xpath("//div[#class='droplist-item' and ./a[.='Headline Category']]").click()
driver.find_element_by_id("rbAfter2006").click()
click(driver.find_element_by_xpath("//a[.='Debt and Structured Products']"))
click(driver.find_element_by_xpath("//a[.='Debt Securities']"))
click(driver.find_element_by_xpath("//a[.='Issuer-Specific Report - Debt Securities']"))
Or you can use specific method for menu selection like below:
def select_menu(menu_text):
element = driver.find_element_by_xpath(f"//a[.='{menu_text}']")
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
driver = webdriver.Chrome()
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml?lang=en")
driver.find_element_by_css_selector(".searchType").click()
driver.find_element_by_xpath("//div[#class='droplist-item' and ./a[.='Headline Category']]").click()
driver.find_element_by_id("rbAfter2006").click()
select_menu("Debt and Structured Products")
select_menu("Debt Securities")
select_menu("Issuer-Specific Report - Debt Securities")
More generic method to select search criterias:
def select_search_criteria(criteria, *menus):
driver.find_element_by_css_selector(".searchType").click()
criteria_element = driver.find_element_by_xpath(f"//div[#class='droplist-item' and ./a[.='{criteria}']]")
data_value = criteria_element.get_attribute("data-value")
criteria_element.click()
document_type_element = driver.find_element_by_id(data_value)
if document_type_element.find_element_by_css_selector("a.combobox-field").get_attribute("aria-expanded") == "false":
document_type_element.click()
for menu in menus:
element = document_type_element.find_element_by_xpath(f".//a[.='{menu}']")
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
driver = webdriver.Chrome()
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml?lang=en")
select_search_type("Headline Category", "Debt and Structured Products", "Debt Securities")
select_search_type("Document Type", "Circulars")

How to click on the list of the <a> elements in an <ul> and <li> elements with selenium using python?

I'm scraping a website. I'm trying to click on a link under <li> but it throws NoSuchElementException exception.
And the links I want to click:
I'm using below code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('window-size=5000x2500')
webdriver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
url = "https://www.cofidis.es/es/creditos-prestamos/financiacion-coche.html"
webdriver.get(url)
webdriver.find_element_by_xpath('//*[#id="btncerrar"]').click()
time.sleep(5)
webdriver.find_element_by_link_text('Préstamo Coche Nuevo').click()
webdriver.save_screenshot('test1.png')
The error I got:
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:10: DeprecationWarning: use options instead of chrome_options
# Remove the CWD from sys.path while we load stuff.
---------------------------------------------------------------------------
NoSuchElementException Traceback (most recent call last)
<ipython-input-44-f6608be53ab3> in <module>()
13 webdriver.find_element_by_xpath('//*[#id="btncerrar"]').click()
14 time.sleep(5)
---> 15 webdriver.find_element_by_link_text('Préstamo Coche Nuevo').click()
16 webdriver.save_screenshot('test1.png')
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py
in find_element_by_link_text(self, link_text)
426 element = driver.find_element_by_link_text('Sign In')
427 """
--> 428 return self.find_element(by=By.LINK_TEXT, value=link_text)
429
430 def find_elements_by_link_text(self, text):
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py
in find_element(self, by, value)
976 return self.execute(Command.FIND_ELEMENT, {
977 'using': by,
--> 978 'value': value})['value']
979
980 def find_elements(self, by=By.ID, value=None):
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py
in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py
in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Préstamo Coche Nuevo"}
(Session info: headless chrome=72.0.3626.121)
(Driver info: chromedriver=72.0.3626.121,platform=Linux 4.14.79+ x86_64)
You could simply grab that url and get to it. Also, worth noting that you have a base url you can simply add the hyphenated search string to i.e. financiar-viaje on to base of https://www.cofidis.es/es/creditos-prestamos/
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
url = 'https://www.cofidis.es/es/creditos-prestamos/financiacion-coche.html'
driver = webdriver.Chrome()
driver.get(url)
url = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "[href*='financiar-viaje']"))).get_attribute('href')
driver.get(url)
Use below code to click on the link
webdriver.find_element_by_css_selector("#ei_tpl_navertical li>a[data='16736']").click()
Use implicit/explicit wait to make sure your element is ready to interact. in your case :
webdriver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
url = "https://www.cofidis.es/es/creditos-prestamos/financiacion-coche.html"
webdriver.get(url)
webdriver.implicitlyWait(20)
webdriver.find_element_by_id('btncerrar').click()
time.sleep(5)
webdriver.find_element_by_css_selector("#ei_tpl_navertical li>a[data='16736']").click()
webdriver.save_screenshot('test1.png')

Categories

Resources