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

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()

Related

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

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'")

Find element by XPATH using Selenium with Python

I am trying to click various buttons on a page using Selenium (in combination with PhantomJS). The html of the button looks like this:
<button class="btn btn-default btn-kudo btn-xs empty js-add-kudo" data-
entry="["Activity",1171944030]" str-on="click" str-trackable-
id="CgwIBTIICN7k6a4EGAESBAoCCAc=" str-type="kudos" title="Give Kudos">
<span class="app-icon icon-dark icon-kudo icon-sm">Kudos</span>
<span class="count count-kudos" data-kudo-count="0">0</span>
</button>
I want to click the buttons with certain Activity IDs that I define earlier in my script. Therefore, I would like to find the button using the XPATH of the data-entry. I tried the following:
driver.find_element_by_xpath('//input[#data-entry="["Activity",1171944030]"]')
and
driver.find_element_by_xpath('//input[#data-entry="["Activity",1171944030]"]')
And a few variations putting the quotation marks in different positions, but none of them is able to find the element. Can anyone see what I am doing wrong?
You can avoid the quotation marks problem if you use contains
driver.find_element_by_xpath("//button[contains(#data-entry, 'Activity')][contains(#data-entry, '1171944030')]");

What is the name of the page element that reads "+ Add telephone" on the Google Hangouts start page?

I'm looking for the name of the page element that reads "+ Add telephone" on Google Hangouts start page, for the purpose of plugging it into Selenium Python API's webdriver.Firefox().find_element_by_name() command.
Alternatively, the element ID would also work, as I know there is also a find_element_by_id() command.
That element does not have any name. Go for xpath contains function and use find_element_by_xpath
//div[contains(text(),'Add telephone')]
However, it's always good idea to not to use Google for any kind of testing. That's kind of trap. Specially for Selenium
Inspecting the element (using Google Chrome), it looks like this:
<div class="c-N-K c-za-b ZG Kn-Mo" role="button" tabindex="0" style="-webkit-user-select: none;">+ Add telephone</div>
There's no name or id on that element. But it is wrapped within a span that has a unique id!
<span id=":sb.og">
<div class="c-N-K c-za-b ZG Kn-Mo" role="button" tabindex="0" style="-webkit-user-select: none;">+ Add telephone</div>
</span>
Alternatively, as #Saifur points out, you can also find by text.

how to pass value into html form in Bottle?

I’m novice to python and Bottle but I’m trying to develop a simple web application which will inventory items in boxes that company receives.
Using Bottle I was able to create a form which has 2 text boxes and one ‘Save’ button. I scan box ID and it get into text box1. Then I scan item ID and it get into text box2. Then I click on Save button.
It works … but after I click on ‘Save’ the form get reloaded i.e. it open blank page and I have to move back page, delete the content from text box1 and do it again until I switch to the next Box which will start with empty box1 and box2
My request: I want that every time I click on ‘Save’ button it submitted data into my database but the form stay intact i.e. not reloaded and the content of text box1 get empty. Then I could just scan next item and so on until I complete all items.
Could please someone help me with that?
Here’s how my code look for now in Bottle template:
<form action="/accession" method="GET">
Scan Box: <input type="text" size="18" name="package"> &nbsp &nbsp &nbsp
Scan Item: <input type="text" size="13" name="sample">
<input type="submit" name="save" value="Save" >
**
I slightly changed the form and now it behaves differently i.e. when I click on "Save" it stays on the same page ( which is OK ) but it empties the content of both text boxes.
I need that only one text box be cleared but another one keep the content. How could I do it?
Thanks
**
I noticed that I could use 'value' attribute with "text" box .. like this:
Scan Box: <input type="text" value="123" name="package">
In my case the value "123" should be dynamic. I do have the value in my python script that I want to replace with "123" but I don't know how to pass it into the form.
Could someone help me?
thanks
You should use a template. Here are the docs for Bottle's built-in templating; I happen to prefer Jinja2 but you can decide which to use once you've mastered the concept.
Basically, you'll create a template file that is the html you want to return. It will include something like this:
Scan Box: <input type="text" value="{package}" name="package">
And your Bottle function (which you haven't posted, so I'm making a guess here) will look something like this:
#route('/myform')
def submit():
the_package = zzz # get the value however your application chooses
return template('form1', package=the_package) # your template file is form1.tpl
The value of the_package will automatically be substituted where {package} appears in your template file.
Please try the template examples in the Bottle documentation and let us know if you have any more questions.

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