How to click and open the calendar on page in Selenium? - python

I am trying to select the date by clicking the below. However when I try to find the element and click I get no errors but the calendar on page doesn't open.
I am unable to figure out what I am doing wrong here or is there a way to directly input the date?
<div class="was-bold-text">To</div>
<div class="was-date-container was-light-text" data-displaydate="End Date">
<input name="" value="" id="was-returns-reconciliation-report-end-date" type="date" class="was-form-control was-input-date" data-defaultdate="" data-mindate="" data-maxdate="today" data-placeholder="End Date" max="2020-02-12" min="2020-02-04">
</div>
I have tried:
driver.find_element_by_id('was-returns-reconciliation-report-end-date').click()
# No error thrown but the calender view doesn't open either.
driver.find_element_by_class_name('was-date-container').click()
# No error thrown but the calender view doesn't open either.
What would be the way to select the date or open the calender and click the date required?

You don't have to click on date picker because its a native input element not plugin like datepicker or datetimepicker. But you can set the value by follwoing method,
datepicker = driver.find_element_by_id('was-returns-reconciliation-report-end-date')
datepicker.clear() # clear values if any.
datepicker.send_keys("02/12/2020")
I think this will work for you.

Have the same problem a while ago and solved by setting the datePicker value by javascript. Its not the click you are looking but might work for your problem in C# is something like this:
((IJavaScriptExecutor)driver).ExecuteScript("document.getElementById('was-returns-reconciliation-report-end-date').value='2020-01-05'");
So in python would be like this I guess, maybe some syntax error:
driver.execute_script("document.getElementById('was-returns-reconciliation-report-end-date').value = '2020-01-05'")

Related

Is using dynamic value on XPATH selenium possible?

I want to click some element (date) in date picker popup using selenium python, if I define the text it can be clicked. but, when I change it with formatting it not worked and I don't know why.
I already tried this way with formatting string in python way:
chosen_date = str(day_date-7)
date_choose = driver.find_element(By.XPATH, "// div[contains(text(), '{}')]".format(chosen_date))
HTML:
<div class="shopee-react-date-picker__table-cell-wrap">
<div class="shopee-react-date-picker__table-cell selected">6
</div>
</div>
but, it never worked and always throw an error and stop the script automatically. I'm quite new in using selenium to automate website, is there any possibilities to overcome this error?
Thank you so much in advance.
as I can see is there is a space in // div section of your selector. There should be not any space:
//div[contains(text()
the rest is ok.

How to find element id by specific text and click on it?

I am trying to write a bot to book a gym/gym classes.
I have managed to get throught login page etc.. but can't figure out how to search for text in element and click on it.
HTML page source:
<input type="submit" name="ctl00$MainContent$ClassStatus$ctrl0$btnBook" value="Book" id="ctl00_MainContent_ClassStatus_ctrl0_btnBook" class="btn btn-success pull-right" data-qai-id="button-ActivityID=B7GYMSB16300321 ResourceID=30212837 Duration=60 Status=Available Date=23/06/2021 16:30:00" />
So when I ask my bot to click on it via element id - it works just fine
driver.find_element_by_id('ctl00_MainContent_ClassStatus_ctrl0_btnBook').click()
The only problem is this element id is not set in stone, it does change for specific time/day/etc...
In html source I can see:
data-qai-id="button-ActivityID=B7GYMSB16300321 ResourceID=30212837 Duration=60 Status=Available Date=23/06/2021 16:30:00"
Is there any way to look for part of that test in data-qai-id = Date=23/06/2021 16:30:00 and then click on element_id containing it??
Many thanks for your time.
No you should use the date format for this purpose :
Date=23/06/2021 16:30:00
cause it going to be different evert time and your code will never work. try the below xpath
//input[contains(#name, 'MainContent_ClassStatus_')]
or
//input[contains(#id, 'MainContent_ClassStatus_') and #value='Book' and contains(#name, '$MainContent$ClassStatus$ctr')]
See if this works:
driver.find_element_by_xpath(".//input[#value='Book']").click()

How to fix a changing xPath in search box (doesn't contain text)

Basically I want to input invoices in Xero software for my job. The process is very simple, I have some values that I need to input in some slots. I have a big problem however. The xpath is dynamic (changes every time you refresh).
Basically it changes from something like this:
//*[#id="PaidToName_12ddc347c7bc4f5aa84c452f55660690_value"]
To something like this:
//*[#id="PaidToName_4fea44e4f8a844b4b630b4bf149490d8_value"]
So the numbers keep on changing.
I have tried a starts-with function however I am pretty sure that there are two XPATHs that starts with PaidToName or end with value, therefore this doesn't seem like a solution as I get this error message:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
The other thing to note is that I see many elements that have the "input type hidden" in the HTML code which I am pretty sure play a role with that. Please let me know if there is anything I can do to help.
This is the code I have tried that doesn't work.
button = driver.find_element_by_xpath("//*[starts-with(#id,'PaidToName')]")
button.send_keys('lol')
This is the HTML code I am trying to retrieve
<input type="text" size="24" autocomplete="off" id="PaidToName_4fea44e4f8a844b4b630b4bf149490d8_value" name="PaidToName_4fea44e4f8a844b4b630b4bf149490d8_value" class="x-form-text x-form-field autocompleter x-form-focus" tabindex="10" style="width: 129px;">
You can use xpath with id and class combination, try this :
button = driver.find_element_by_xpath("//*[contains(#id,'PaidToName') and contains(#class,'x-form-text')]")
button.send_keys('lol')
Try below given locator.
driver.find_element_by_xpath("//input[contains(#id,'PaidToName') AND contains(#class,'x-form-text')]")

Selenium not clicking element

I have the following checkbox element:
<input checked="checked" class="iceSelBoolChkbx"
id="mainContentId:newSerchCheckBox" name="mainContentId:newSerchCheckBox"
onblur="setFocus('');"
onclick="var form=formOf(this);iceSubmitPartial(form, this, event);"
onfocus="setFocus(this.id);"
onkeypress="var form = formOf(this);
Ice.util.radioCheckboxEnter(form,this,event);"
type="checkbox">
I currently am locating the element and attempting to click it using
check_box = driver.find_element_by_id('mainContentId:newSerchCheckBox')
check_box.click()
When I run the code it runs without any errors but on the site the checkbox is still unchecked. What could be causing this and is their an alternative way to check the box?
Try replacing click with mousedown, I don't know what the underlying difference is but it resolved the issue I was having with click().
driver.execute_script('$("#mainContentId:newSerchCheckBox").trigger("mousedown")')
Let me know if you're thrown an error and we can re-write it in pure Javascript rather than relying on jQuery.
I think your checkbox is already selected if you see the input tag checked="checked".So you need to check first whether selected or not.If selected do not click.
element=driver.find_element_by_id("mainContentId:newSerchCheckBox")
if element.is_selected():
print('Check box is already selected')
else:
element.click()

Using Selenium in Python to click/select a radio button

I am trying to select from a list of 3 buttons, but can't find a way to select them. Below is the HTML I am working with.
<input name="pollQuestion" type="radio" value="SRF">
<font face="arial,sans-serif" size="-1">ChoiceOne</font><br />
<input name="pollQuestion" type="radio" value="COM">
<font face="arial,sans-serif" size="-1">ChoiceTwo</font><br />
<input name="pollQuestion" type="radio" value="MOT">
<font face="arial,sans-serif" size="-1">ChoiceThree</font>
I can find it by using the following code:
for i in browser.find_elements_by_xpath("//*[#type='radio']"):
print i.get_attribute("value")
This outputs: SRF,COM,MOT
But I would like to select ChoiceOne. (To click it) How do I do this?
Use CSS Selector or XPath to select by value attribute directly, then click it.
browser.find_element_by_css_selector("input[type='radio'][value='SRF']").click()
# browser.find_element_by_xpath(".//input[#type='radio' and #value='SRF']").click()
Corrections (but OP should learn how to look up in documentation)
In Python binding, find_elements_by_css doesn't exist, it's called find_elements_by_css_selector. One should be able to look at the exception message and look back into documentation here and figure out why.
Notice the difference between find_element_by_css_selector and find_elements_by_css_selector? The first one finds the first matching element, the second one finds a list, so you need to use [0] to index. Here is the API documentation. The reason why I use the latter, is because I copied your code, which I shouldn't.
Selenium webdriver Radio button click
When i used xpath :
driver.find_element_by_xpath("//input[#id='id_gender2']").click()
radio button not selected
But I used css_selector :
driver.find_element_by_css_selector("input#id_gender1").click()
radio button selected
find_elements_by_css_selector worked for me,
browser.find_elements_by_css_selector("input[type='radio'][value='SRF']")[0].click()
First Radio button was not selected for me also. But after inserting Time it works for me.
driver.find_element_by_class_name("login").click()
driver.find_element_by_id("email_create").send_keys("testsel000#gmail.com")
driver.find_element_by_id("SubmitCreate").click()
time.sleep(2)
driver.find_element_by_css_selector("#id_gender2").click()
Consider that you have a radio button to select either of the two options, "Male or "Female". Then try using the following :- This is in Python (Selenium).
driver.find_element_by_xpath("//label[contains(text(),'Male')]").click()
browser.find_elements_by_xpath(".//input[#type='radio' and #value='SRF']")[0].click
This ended up being the fix. I was getting errors without the [0] there, that a list does not have a click() attribute (even though there was only 1 match). Thanks for the help user1177636!

Categories