0

**How can I click a non-button for refresh page - ** I'm trying to web scraping with selenium but I cant click a spesific area. Here is my code and ERROR message.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
from userinfo import username, password

class demo:
    def __init__(self,username,password):
        self.username = username
        self.password = password
        self.browser = webdriver.Firefox(executable_path='./geckodriver')
        
    def sign(self):
        url = 'example url'
        self.browser.get(url)
        time.sleep(5)
        usernameInput = self.browser.find_element('name','frmLoginPanel:inpUser')
        passwordInput = self.browser.find_element('name','frmLoginPanel:inpPass')

        usernameInput.send_keys(username)
        passwordInput.send_keys(password)
        
        passwordInput.send_keys(Keys.ENTER)
        
        time.sleep(4)
        

        elmnt = self.browser.find_element(By.XPATH, '//*[@id="frmCounters:counterList:0:btnUpdateCounter"]')
        actions = ActionChains(elmnt)
        actions.click(elmnt).perform()
            
app = demo(username,password)

app.sign()
        

Here is Website Element image link: https://www.amazon.com/photos/shared/7n8SF26YTWCu_Ws_OQe4Kw.HfpXhpcXVewL5nAf3DBOD5


[1]NoSuchElementException: Unable to locate element: //*[@id="frmCounters:counterList:0:btnUpdateCounter"]

  • The exception is telling you that the id can not be found. If that button is loaded via JS you can try wrapping the find element with a `wait until`: `element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, "")) )` – nck Nov 05 '22 at 10:02
  • Thanks boss. I am grateful. It's worked. You saved life :) – Şakir Bayram Nov 05 '22 at 13:14

1 Answers1

0

As stated in the comments, sometimes elements take some time to load if they are built through JS therefore including some explicit wait can help:

element = WebDriverWait(driver, 10).until(
      EC.presence_of_element_located((By.XPATH, "<your_xpath>")
    )     
)

Where 10 is the number of seconds you allow to wait until a TimeoutException is thrown.

nck
  • 1,673
  • 16
  • 40