Button Click element issue in selenium using python - python

I am trying to click button(name command page) on web page but i am unable to do so. i am using selenium with python
code:
wait= WebDriverWait(driver,20)
command_page = wait.until(EC.element_to_be_clickable((By.ID,"Button_ID")))
command_page.click()
I have tried by class name also but i am unable to click the element.
Please help me on this.

As an alternative you can use JavascriptExecutor to perfrom click on certain element if Selenium click() method doesn't trigger the action without any Exception.
element = driver.find_element_by_id("etoolbar_toolbarSection_newcommandpagebtn_id")
driver.execute_script("arguments[0].click();", element)

Please try below solution :
WebDriverWait(driver, 20)
both_button=wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Command Page')]")))
both_button.click()

I tried this, seems to be working
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("file://c:/cygwin64/home/das2/abcd.html")
element = driver.find_element_by_id("etoolbar_toolbarSection_newcommandpagebtn_id")
element.click()

Related

Selenium Python is unable to locate an element

I'm new to webdrivers and am experimenting with them.
I'm trying to click a button when opening a webpage and it keeps giving the error of unable to locate element.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("html page")
button = driver.find_element(By.ID, "onetrust-accept-btn-handler")
button.click()
i have tried id and xpath but i dont know what else to use.
the path for the button is:
/html/body/div[3]/div[3]/div/div/div[2]/div/div/button
<button id="onetrust-accept-btn-handler" tabindex="0">Run</button>
You can use implicit wait for wait until the page is fully loaded
driver.implicitly_wait(10)

selenium.common.exceptions.NoSuchElementException in Selenium

I am trying to make Selenium automatically open a webpage in Chrome.
This is my code...
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.python.org/")
element = driver.find_element_by_link_text("q")
print(element.click())
This is the error...
selenium.common.exceptions.NoSuchElementException
Pls help.
I see there are 2 problems here:
You have to add delay before element = driver.find_element_by_link_text("q") to let the page load.
Preferably you should use visibility_of_element_located expected condition for this.
I see no element with link text equals to q there

Unable to locate element in Python Selenium webdriver find_element_by_xpath

i'm trying to click the "download Results" button on this website .
i'm using below python code to click this button
from selenium import webdriver
chromedriver_path = 'E:/software/python/chromedriver'
url = 'https://www.cms.gov/apps/physician-fee-schedule/search/search-results.aspx?Y=0&T=4&HT=2&CT=3&H1=74750&H2=74800&M=5'
driver= webdriver.Chrome(executable_path=chromedriver_path )
driver.get(url)
driver.find_element_by_xpath('//*[#title="Download Results"]').click()
i'm getting below error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//frame[#name="aspnetForm"]"}
(Session info: headless chrome=83.0.4103.116)
i'm thinking if the button is within a iframe but how do I find out the iframe?
This may help you out: Unable to locate element using selenium webdriver in python
Try and switch to the frame first, and then search for the element.
i realized that there's a agreement page that i need to click 'Agree' button first.
i didn't see this in browser because i already clicked 'Agree' weeks ago. but in webdriver, i need to click that each time.
Just after the URL opens, a page comes up asking you to agree the terms. You need to click on that Agree first. Since it is possible that once clicking on the Agree button, it won't come again if you accepted it on default browser. So just be sure with code, you can first check the presence of the agree button first.
This kind of function you may make to check presence:
def elementPresent(locatorType, locator):
#present = true
#not present = false
wait = WebDriverWait(driver, 20)
try:
wait.until(EC.presence_of_element_located((locatorType, locator)))
except Exception:
return False
return True
And then using if condition you may proceed further like:
if(elementPresent("xpath", "//a[#title='Accept']/span")):
driver.find_element_by_xpath("//a[#title='Accept']/span").click()
and then you may click on the element you want, there is no frame that is required to be switched to.
driver.find_element_by_xpath("//a[#title = 'Download Results']/span").click()
Just extract the element and click on it and your required file will be downloaded.

Python Selenium click on element by xpath

I can locate the XPATH of the element that I want, however it will not allow me to click on it. In specific, it throws a "WebDriverException."
from selenium import webdriver
browser=webdriver.Chrome()
url='https://fred.stlouisfed.org/categories/32261'
browser.get(url)
click=browser.find_element_by_xpath("//a[#title='next page']")
print(click.get_attribute('title'))
click.click()
Returns the following error:
You cannot click on required element because it's not visible currently. You should scroll down to "Next" button before clicking it:
from selenium import webdriver
browser = webdriver.Chrome()
url = 'https://fred.stlouisfed.org/categories/32261'
browser.get(url)
next_button = browser.find_element_by_xpath("//a[#title='next page']")
browser.execute_script("arguments[0].scrollIntoView();", next_button)
next_button.click()
So, the XPath was there, however I don't believe I was actually pointed to it when trying the initial "click.click()." There might be a better solution, however this seems to be working for now.
from selenium import webdriver
browser=webdriver.Chrome()
url='https://fred.stlouisfed.org/categories/32261'
browser.get(url)
click=browser.find_element_by_xpath("//a[#title='next page']")
print(click.get_attribute('title'))
click.send_keys('next page')
click.click()

Unable to click the linktext using selenium execute_script function using selenium python

Unable to click the linktext using selenium execute_script function
This is what I am tring to do:
self.driver.execute_script("document.getElementByLinktext('Level 1s').click;")
You are not calling the click() method:
self.driver.execute_script("document.getElementByLinktext('Level 1s').click();")
FIX HERE^
Note that you can also locate the element with selenium and then pass it into the script:
link = self.driver.find_element_by_link_text('Level 1s')
self.driver.execute_script("arguments[0].click();", link)
You can also perform the click via selenium directly if applicable:
link.click()
Also related:
WebDriver click() vs JavaScript click()

Categories

Resources