<input class="jss1805" name="confirmWeight" tabindex="0" type="checkbox" data-indeterminate="false" value="">
The code^ is from a html website and its a checkbox which I want to click. Except it's an input rather than a button or td. Am I able to click this as when I run selenium it seems to not find it.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[2]/main/form/div/div[2]/div[4]/div[2]/div/div/div[2]/div/div[1]/div/div/label/span[1]/span/input"))).click()
This finds the element but doesn't click it.
Try the following:
from selenium.webdriver.support.wait import WebDriverWait
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name=confirmWeight]")))
element = driver.find_element_by_css_selector("input[name=confirmWeight]")
element.click()
Explanation:
First you need to wait till your element is clickable. For this you have to define the locator which is unique.
Find the element and click it
If you want to make an input use:
element.send_keys("`your input")
If you want to click on svg, that is a child use: input[name=confirmWeight]>svg
I don't see it in your html. It looks like the info you provided is not enough.
Related
I am working on a program that automates logs into to a certain webpage and clicks certain buttons & links to reach a final destination to enter certain values and submit them. I have managed to navigate through the webpages but one of the webpages has a hyperlink button that I need Selenium to click, however, after trying multiple different methods, I cannot get it to work.
I have tried finding the element with By.XPATH, By.LINK_TEXT, By.PARTIAL_LINK_TEXT and none of these worked. I thought my issue might be that since it is clicking onto a totally new URL, so I load the new URL towards the bottom of my code to then move forward with my program.
The hyperlink button:
Button
The chunk of code to the hyperlink button I am trying to click on:
The XPath itself is : /html/body/div[2]/table/tbody/tr/td[2]/p/span/a[2]
driver = webdriver.Chrome(executable_path='C:\chromedriver.exe')
driver.get('')
'''
username_input = '//*[#id="userNameInput"]'
password_input = '//*[#id="passwordInput"]'
submit_button = '//*[#id="submitButton"]'
send_push = '//*[#id="auth_methods"]/fieldset/div[1]/button'
'''
# enters username and password into fields
driver.find_element("xpath", '//*[#id="userNameInput"]').click()
driver.find_element("xpath", '//*[#id="userNameInput"]').send_keys(username)
driver.find_element("xpath", '//*[#id="passwordInput"]').click()
driver.find_element("xpath", '//*[#id="passwordInput"]').send_keys(password)
driver.find_element("xpath", '//*[#id="submitButton"]').click()
# clicks 'send me a push' button on duo mobile screen
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='duo_iframe']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(("xpath", "//button[normalize-space()='Send Me a Push']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(("xpath", '//*[#id="p_p_id_56_INSTANCE_xWhKj4tIFYvm_"]/div/div/div[1]/a[5]'))).click()
# loads next url which has the link on its webpage that needs to be clicked
driver.get('')
# attempts to click on link
driver.find_element("xpath", '/html/body/div[2]/table/tbody/tr/td[2]/p/span/a[2]').click()
I have removed the URLs in driver.get('') as they contain sensitive URLs
My last line of code is my attempt to click the hyperlink using the XPath
Any help is appreciated!
EDIT:
After further investigation, I now see that 2 buttons both have the same class name in their elements:
The SITE MAP button: SITE MAP
The Student button: Student
Considering the HTML:
The element with text as SITE MAP is a <a> tag and a dynamic element. To click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "SITE MAP"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.submenulinktext2"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='submenulinktext2' and contains(., 'SITE MAP')]"))).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 assume find_element does not return the correct element hence cannot click. Or you can verify it by changing .click() to .text. If it returns SITE MAP, then it returns a correct element.
I have another solution if you can try. It's using xpath and assumes class='pageheaderlink' is a single element in the HTML body.
list_subcontent = driver.find_elements_by_xpath(".//span[#class='pageheaderlink']")
for item in list_subcontent:
if item.text == 'SITE MAP':
item.click()
I'm using Selenium Webdriver to check a box in a form and click save on the same page. The checkbox works fine, but cannot click save. I have tried multiple solutions without a working solution. I add a sleep as after clicking save manually it does take a while to process.
Here is the html for the save button:
<input class="cbi-button cbi-button-apply" type="submit" name="cbi.apply" value="Save">
Here is my code using xpath (attempt 1):
driver.find_element_by_xpath("//input[#type='submit']").click()
time.sleep(20)
Output from attempt 1:
ElementNotInteractableException: Message: element not interactable
Attempt 2 using action chains as suggested by another answer:
button = driver.find_element_by_class_name(u"cbi-page-actions")
driver.implicitly_wait(10)
ActionChains(driver).move_to_element(button).click(button).perform()
time.sleep(20)
There are no errors raised in this second attempt but from watching the browser the save button did not seem to be clicked. I have also checked afterwards on the page and the changes I added were definitely not saved.
I've also attempted using webforms however I have the opposite problem where I am able to save the form but cannot select the checkbox.
Try this
element = driver.find_element_by_xpath("//input[#type='submit']")
driver.execute_script("arguments[0].click();", element)
You should wait before, may be the element was not loaded properly, not after triggering the click
Solution 1: Direct click with worst type of Explicit wait
time.sleep(20)
driver.find_element_by_xpath("//input[#type='submit']").click()
Solution 2: Using Actions chain :
button = driver.find_element_by_css_selector("input[value='Save']")
ActionChains(driver).move_to_element(button).click(button).perform()
Solution 3: Using dynamic Explicit wait:-
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.NAME, "cbi.apply"))).click()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Solution 4: Using Js
button = driver.find_element_by_name("cbi.apply")
driver.execute_script("arguments[0].click();", button)
I just started working with Selenium webdriver to try automate clicking a button in a discord chat based on preconditions. The problem I am having is being able to classify this button.
For starters, the HTML code is as follows:
<div tabindex="0" class="reactionInner-15NvIl" aria-label="♥️, press to react" aria-pressed="false" role="button"><img src="/assets/1afd3c799f3e158662a70498e83e2a87.svg" alt="♥️" draggable="false" class="emoji"><div class="reactionCount-2mvXRV" style="min-width: 9px;">1</div></div>
What I first tried to do was to find_element_by_xpath:
driver.find_element_by_xpath('/html/body/div/div[1]/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div[1]/div[1]/div/div/div[48]/div[1]/div[2]/div[2]/div[1]/div/div').click()
But, when a new opportunity for this reaction comes up, the xpath changes:
/html/body/div/div[1]/div/div[2]/div/div/div/div[2]/div[2]/div[2]/div[1]/div[1]/div/div/div[50]/div[1]/div[2]/div[2]/div[1]/div/div
Notice the only part changing it the div[48] to div[50]
The next thing I tried was to find_element_by_class_name:
element = driver.find_element_by_class_name('reactionInner-15NvIl')
driver.execute_script("arguments[0].click();", element)
The reason I did this was because I was having a problem with simply doing:
driver.find_element_by_class_name('reactionInner-15NvIl').click()
That code would give me an error saying Message: element click intercepted: Element <div tabindex="0" class="reactionInner-15NvIl" aria-label="💗, press to react" aria-pressed="false" role="button">...</div> is not clickable at point (405, 94). Other element would receive the click: <span id="NewMessagesBarJumpToNewMessages_122" class="span-3ncFWM">...</span> and the code wouldn't run. The program runs with the execute_script but it just isn't doing anything, the button is not being clicked. So if anyone has any idea how to be able to click this button, any help would be appreciated!
You can use xpath in a better way than navigate between the divs, like you did the first time.
First of all, if you can change the HTML to use id, or name, thats better.
In this case, you could use this xpath:
//div[#class = "reactionInner-15NvIl"]
When searching by class, this could return multiple results, so use the function find_elements_by_xpath, and then choose the exactly one
Induce WebDriverWait and wait for element_to_be_clickable() and following css selector.
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[class*='reactionInner-'][role='button']"))).click()
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Update:
Induce Javascript Executor to click.
driver.execute_script("arguments[0].click();", WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[class*='reactionInner-'][role='button']"))))
I know I am doing something wrong. I need to create software to test a website. send_keys is working just fine for the rest of my code but I am not sure what I am doing wrong in this section. I have tried everything I can think of. I am very new to python and selenium so it's probably something silly.
Things I have tried:
1.
elem = lambda: driver.find_element_by_xpath('//textarea[#aria-label="Comment"]').click()
elem().click()
elem.send_keys("this is a comment")
elem.send_keys(Keys.RETURN)
2.
elem = lambda: driver.find_element_by_xpath('//span[#aria-label="Comment"]').click()
elem().click()
elem.send_keys("this is a comment")
elem.send_keys(Keys.RETURN)
3.
com_elem = driver.find_element_by_xpath('//textarea[#aria-label="Add a comment…"]')
com_elem.clear()
com_elem.send_keys("comment")
I have tried every combination I can think of and I know it needs to match the HTML but I have still tried it with lambda and without, with span and textarea (since it has a button you press that places your cursor in the text box)
I do not know what else to try.
All it needs to do is click the text box, add words and hit enter.
this is HTML for the text box it needs to go inside of:
<form class="X7cDz" method="POST">
<textarea aria-label="Add a comment…" placeholder="Add a comment…" class="Ypffh" autocomplete="off" autocorrect="off" style="height: 18px;"></textarea>
<button class="sqdOP yWX7d y3zKF " type="submit">
Post
</button>
</form>
This is HTML for the button that it can click and the cursor will go in the box:
<span class="_15y0l">
<button class="dCJp8 afkep">
<span aria-label="Comment" class="glyphsSpriteComment__outline__24__grey_9 u-__7">
</span>
</button>
</span>
This part of the website works perfectly. I know because I have tested it by hand.
The desired element is a JavaScript enabled element so to invoke send_keys() 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:
elem = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea[aria-label^='Add a comment'][placeholder^='Add a comment']")))
elem.send_keys("Daniela Ciro")
elem.send_keys(Keys.RETURN)
Using XPATH:
elem = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//textarea[starts-with(#aria-label, 'Add a comment') and starts-with(#placeholder, 'Add a comment')]")))
elem.send_keys("Daniela Ciro")
elem.send_keys(Keys.RETURN)
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 don't think you need to use lambda to complete this task. You're on the right track, you need to first identify the path of the textbox and set it to a variable so your send_keys() will know where to input the text. You then need to find the enter button's path and click it. Something along the lines of this should help:
elem = driver.find_element_by_xpath('textbox xpath here')
elem.send_keys('what you want to put into textbox')
driver.find_element_by_xpath('your enter button xpath here').click()
If you could provide the website or an example, it would be possible for me to give more specific code.
I have a button, i can successfully find it and capture it's text. However click is not working. Can someone help.
<button class="yellow labeled icon button no-margin" type="button">
<div class="icon my_class"></div>
<span class="ttt">Add new student</span>
</button>
This works:
return driver.find_element(By.XPATH, 'xpath').text
But this does not work and "no exception". Code pass but click not done.
driver.find_element(By.XPATH, 'xpath').click()
You can try clicking it with javascript :
element = driver.find_element(By.XPATH, 'xpath')
driver.execute_script("arguments[0].click();", element)
Of-coarse invoking .text and click() relates to two different states of a WebElement.
Your code block return driver.find_element(By.XPATH, 'xpath').text succeeds as the text Add new student gets rendered within the HTML DOM when you lookout for it. This doesn't guarantees that the WebElement is also interactable by that time. There is a possibility of a JavaScript/Ajax at work rendering the button at that point of time.
It is worth to mention that rendering of the text Add new student resembles to the expected_conditions clause of text_to_be_present_in_element where as interactability (i.e. clickability) resembles to the expected_conditions clause of element_to_be_clickable
Solution
As you mentioned it's an AngularJS based site/element and the click is not getting executed you have to induce WebDriverWait in-conjunction with expected_conditions clause set as element_to_be_clickable as follows :
WebDriverWait(self.driver, 30).until(EC.element_to_be_clickable((By.XPATH, "xpath"))).click()