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()
Related
I have a problem. I want to find the login page of several websites using Selenium. To do this, I try to go to the page and click the button with text such as "sign in", "login", and so on. This worked for Netflix. Example:
Code Website: Netflix.com
Sign In
My Code to detect the button:
result = "https://www.microsoft.com"
driver.get(result)
elem = driver.find_element_by_link_text('Sign In').click
print(driver.current_url) # returns the website: https://www.netflix.com/de-en/login
Now I try this with the website Microsoft.com. Website code as image:
If I now change my contains query to:
driver.find_element_by_link_text('Sign in').click # small "in"
Selenium does not find any element. I have tried many different options that Selenium offers such as:
find_element_by_id
find_element_by_name
find_element_by_link_text
...
eg: [https://selenium-python.readthedocs.io/locating-elements.html]
My goal is to use the textual information like "sign in, login" etc. to find the button and press it.
I would be very grateful for your help
See the issue here is with Netflix.com , you have a HTML like this :
Sign In
see the tag, it is a, which is an anchor tag in HTML.
find_element_by_link_text, or find_element_by_partial_link_text look for text between anchor tag.
But when you go to Microsoft.com
<div class="mectrl_header_text mectrl_truncate">Sign in</div>
Sign in is wrapped inside div. so find_element_by_link_text or find_element_by_partial_link_text will not work.
instead you can try with below xpath :-
//div[text()='Sign in']
in code :-
driver.find_element_by_xpath("//div[text()='Sign in']").click()
or you can give it a try with Explicit waits as well :-
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Sign in']"))).click()
As described by cruisepandey for these two particular cases, find_element_by_link_text will work in some cases but in some other cases will not.
While locating element by it's text will work for all these case.
So I never use find_element_by_link_text or by partial text methods, just using find_element_by_xpath method based on element's text and it works fine.
In your case you can use
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Sign in']"))).click()
This will be more simple, stable and reliable.
<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.
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 try to click on the next page link on this webpage: https://www.kumon.co.uk/find-a-tutor/
This page will be empty till you type a location like London, Manchester or wherever :)
The source-code looks like this:
<nav class="visible-xs-block">
<ul class="pager">
<li>
<a data-page="2" href="https://www.kumon.co.uk/find-a-tutor/?centre_search=london&page=2"><small><i class="fa fa-chevron-right"></i></small></a>
</li>
</ul>
</nav>
I try to make a click to the next page in 2 ways:
First trial:
browser.find_elements_by_xpath("//*[#class='fa fa-chevron-right']/../..")[2].click()
Error:
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
Second trial:
WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, "//*[#class='fa fa-chevron-right']/../.."[2]))).click()
Error:
selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
What matters is that I can't find any documentation regarding the second error message, and that's why I post my first message on stackoverflow :)
FYI when I remove the click() the element is well localized so this is the click() which sucks seemingly :/
I have found out this solution dealing with Javascript which may be cool for me but I don't really know how to implement this => Selenium Element not visible exception
What do you think about it ?
Do you have any idea to help me please ? i'm stuck on this issue for 2 days unfortunately :(
EDIT:
This snippet:
while browser.find_element_by_xpath("//ul[#class='pagination']//li[last()]/a/small"):
action = ActionChains(browser)
search_button = WebDriverWait(browser, 20).until(
EC.element_to_be_clickable((By.XPATH, "//ul[#class='pagination']//li[last()]/a/small")))
action.move_to_element(search_button)
sleep(1)
search_button.click()
raises an error selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
And I'm not sure that using try...except is the best solution to solve it, is it ?
Thank's in advance,
Nicolas.
To invoke click() on the > icon to move to the next page you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[#class='pagination']//li[last()]/a/small"))).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
Looks xpath which used is not correct. Its good to cross check xpath in browser console. xpath provided in above answer #DebanjanB is good. try once: in chrome->F12->Console: paste below one and Enter.
$x("//ul[#class='pagination']//li[last()]/a/small")
It has to fetch required element. Similarly try all xpaths which you used in posted question, they are fetching wrong elements which leads to exceptions on execution.