I want to click button
<p class="btn" id="LargeNextBtn" style=""><img src="//ticketimage.globalinterpark.com/ticketimage/Global/Play/onestop/G2001/btn_next_on.gif" id="LargeNextBtnImage" alt=""> </p>
but when I use find_element
driver.find_element("id", "LargeNextBtn").click()
can't find "LargeNextBtn"
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="LargeNextBtn"]"}
I also try to into iframe but it also fail
WebDriverWait(driver, 9).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"ifrmBookStep")))
Any help or pointers is appreciated, thank you!
To click on the element you can use either of the following locator strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "p.btn#LargeNextBtn > a#LargeNextBtnLink > img#LargeNextBtnImage").click()
Using xpath:
driver.find_element(By.XPATH, "//p[#class='btn' and #id='LargeNextBtn']/a[#id='LargeNextBtnLink']/img[#id='LargeNextBtnImage']").click()
Ideally to click on the clickable element instead of presence_of_element_located() 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, "p.btn#LargeNextBtn > a#LargeNextBtnLink > img#LargeNextBtnImage"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//p[#class='btn' and #id='LargeNextBtn']/a[#id='LargeNextBtnLink']/img[#id='LargeNextBtnImage']"))).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 need to identify the p tag.
You also need to change the style attribute to display : block use javascripts executor to change the style of the element.
Code:
pTag=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID,"LargeNextBtn")))
driver.execute_script("arguments[0].style.display = 'block';", pTag)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a#LargeNextBtnLink"))).click()
The element you want is not inside the IFRAME so you don't have to switch contexts. Instead of targeting the P tag, I would instead target the A itself since it has an ID.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "LargeNextBtnLink"))).click()
Related
I'm trying to access some elements inside of iframe but withour success. Basically, I found the iframe, switch to it, but I can get the element inside of it. All elements that I try, I always get a message saying that the element was not found.
Could you please help?
HTML:
You can use the below code to switch to frame and then interact with the desired element :-
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[contains(#sandbox,'allow-popups-to-escape-sandbox')]")))
ele = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.analytics-ui-application")))
#ele is a web element so you can trigger .click, .text or any other web element methods on it.
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
As the elements are 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:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame-router[activeclient='analyticsUi'] > iframe[sandbox]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "css_clickable_element"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame-router[#activeclient='analyticsUi']/iframe[#sandbox]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "xpath_clickable_element"))).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
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
Hello I am trying to use selenium to find a button to click on. Below is a snippet of the HTML code i am working with.
<input type="button" id="runButton" class="button" value="Run Report" onclick="chooseRun()">
I am trying to click on the runButton with the code below.
elem = driver.find_element_by_id('runButton').click()
I am getting the following error message:
NoSuchElementException: Message: Unable to find element with css selector == [id="runButton"]
Not sure what else to try.
Most likely what you'll need to do to find your element is to use waits. You need to allow time for an element to be visible, clickable, etc. before you can interact with it. You can find information on waits here: https://selenium-python.readthedocs.io/waits.html
Taken from the above website:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
elem = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "runButton"))
If waits do not work then it's possible that your element is inside an iframe. You will need to switch to that iframe first and then search for the element in order to find it.
You will find the iframe like you would another element and then switch to it like this:
iframe = driver.find_element_by_id("content_Iframe")
driver.switch_to.frame(iframe)
button = driver.find_element_by_id("runButton")
button.click()
Once you're done with the iframe and it's contents, you will need to switch back out of it:
driver.switch_to.default_content()
The element seems to be a dynamic element so to click() on the element you need to use 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, "input.button#runButton[value='Run Report']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='button' and #id='runButton'][#value='Run Report']"))).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
I have tried many ways but driver doesnot clicking my element.
done=driver.find_element_by_xpath("//button[#class='dimapply-btn']")
driver.execute_script("arguments[0].click();", done)
and I have also tried with WebDriverWait.
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Done']"))).click()
I have also used (x,y) point.
done = driver.find_element_by_xpath('//body')
actions.move_to_element_with_offset(done, 320, 430).click().perform()
My html code is:
<div class="btn-apply" data-ng-click="applyDimensionChanges()" role="button" tabindex="0">
<button class="dimapply-btn">Done</button>
</div>
It does not throws any errors. just passes by my code and does not click.
please help me out. thank you in advance. i am solving this line for two days.
To click() on the element with text as Done button you can use either of the following Locator Strategies:
Using css_selector:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.btn-apply[data-ng-click^='applyDimensionChanges']>button.dimapply-btn"))).click()
Using XPATH:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[#class='btn-apply' and starts-with(#data-ng-click, 'applyDimensionChanges')]/button[#class='dimapply-btn' and text()='Done']"))).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
I'm trying to check a checkbox via selenium in Chrome using python3.
This is the HTML code:
<header class="list-header">
<aside class="list-header-bulk-selection">
<input type="checkbox" class="sc-cSHVUG iAwiCZ">
::after
I'm trying to check the box by:
check_mark = driver.find_element_by_xpath("//input[#class='sc-cSHVUG iAwiCZ']")
check_mark.click()
I am able to find the location, but unfortunately I get the following error message:
ElementNotInteractableException: Message: element not interactable
(Session info: chrome=75.0.3770.142)
I think I have to access the ::after line, but I have no clue how I should do this.
Try following options to click on checkbox.
Option1:
location_once_scrolled_into_view
check_mark = driver.find_element_by_xpath("//input[#class='sc-cSHVUG iAwiCZ']")
check_mark.location_once_scrolled_into_view
check_mark.click()
Option2:
WebDriverWait and element_to_be_clickable
check_mark =WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[#class='sc-cSHVUG iAwiCZ']")))
check_mark.click()
Option3:
Java Scripts Executor
check_mark =WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[#class='sc-cSHVUG iAwiCZ']")))
driver.execute_script("arguments[0].click();", check_mark)
You need to import followings to execute above code.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
This sc-cSHVUG iAwiCZ looks suspicious and most probably it's automatically generated each time you refresh the page. I would recommend to stick to something more "static", for example the checkbox should have some label or other text explaining what it does and you could stick to that text.
From what you've provided so far it might be better to locate this <aside> tag first and then use child axis to reach the checkbox like:
//aside[contains(#class, 'bulk-selection')]/child::input
More information:
XPath Tutorial
XPath Axes
XPath Operators & Functions
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 solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "header.list-header>aside.list-header-bulk-selection>input[type='checkbox']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//header[#class='list-header']/aside[#class='list-header-bulk-selection']/input[#type='checkbox' and #class]"))).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
Thank you guys so much. The thing was that ::after is a pseudo-element which is not interactable by selenium.
I actually solved it by accessing the checkbox and then hoovering over it (just a tiny bit) to sort of activate the HTML object. Then the checkbox was interactable.
from selenium.webdriver import ActionChains
check_mark = driver.find_element_by_xpath("//input[#class='sc-cSHVUG iAwiCZ']")
ActionChains(driver).move_to_element_with_offset(check_mark,0.001,0.001).click().perform()
I'm trying to set up an automation that can help me work easier. I want to log in to a device automatically and set up some initial settings. The part I am having trouble with is that I have to check a checkbox to continue, but I couldn't find a way to click on the button.
This is the XPath for that button:
//*[#id="tableHdd"]/div/div[1]/span[1]/input
broswer.find_element_by_class_name('table-cell').find_elements_by_class_name('checkbox').click()
To clcick() on the checkbox you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#tableHdd > div.table > div.table-header > span.table-cell > input.checkbox"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='tableHdd']/div[#class='table']/div[#class='table-header']/span[#class='table-cell']/input[#class='checkbox']"))).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're misusing Element Locators
find_element_* should return a single unique WebElement. If the locator matches > 1 element - the first match will be returned.
find_elements_* - returns the List of WebElements, you cannot invoke click() function from the list
So if there is only one checkbox at the page - you can tick it like:
driver.find_element_by_class_name("checkbox").click()
If there are more checkboxes and you need to tick particular that one - use slightly amended XPath expression:
driver.find_element_by_xpath("//div[#id='tableHdd']/descendant::*/span[#class='table-cell']/input").click()