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')]");
Related
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()
I am trying to turn on/off a toggle switch using the selenium code. The issue is that I would like to access the toggle switch by its "text label" rather than its ID or Name. Here is the HTML for the switch
<input type="checkbox" name="ot-group-id-C0002" class="switch-checkbox category-switch-handler" id="ot-group-id-C0002" aria-checked="true" aria-controls="ot-desc-id-C0002" aria-labelledby="ot-header-id-C0002" data-optanongroupid="C0002" checked="true" tabindex="0">
<label class="switch-label" for="ot-group-id-C0002">
<span class="switch-inner"></span>
<span class="switch-nob"></span>
<span class="label-text">Performance Cookies</span>
</label>
I would like to access the toggle switch via the text "Performance Cookies" rather than its ID or Name.
you can read my answer to a similar question here for details to use xpath in selenium.
additional info i didn't mention there but would help you:
to search by text we use:
//*[text()='Performance Cookies']
to get its parent(label) we use:
ancestor::label
to get the preceding(the tag/element before) which is an input we use
preceding::input
now combine all that and we get:
//*[text()='Performance Cookies']/ancestor::label/preceding::input[#type='checkbox']
Could someone please tell me how can I select a dynamic element using selenium?
I would like to select the "limit-order" element.
<div class="tab-control" id="uniqName_0_85" widgetid="uniqName_0_85">
<span data-tab="market-order" class="tab-item tab-active">Market</span>
<span data-tab="limit-order" class="tab-item">Limit</span>
<span data-tab="stop-order" class="tab-item">Stop</span>
<span data-tab="stop_limit-order" class="tab-item">Stop Limit</span>
</div>
I tried this but no luck:
btn_limit_name_xpath = '//div[contains(#class,"tab-control")]/span[2]'
btn_limit = browser.find_element_by_xpath(btn_limit_name_xpath)
btn_limit.click()
What sometimes does the job for me is copy the full xpath instead of the shorter one.
If that doesn't work either, you could try and check this out.
They show you how you can use an xpath to find a specific piece of text and select the object in that way. So in your case you could try and find it by searching for 'limit'.
I have been using selenium for a little bit now and have been stuck on this issue for the past few hours. It seems really simple but I just can't seem to figure out a proper execution. I basically am trying to select a shoe size based on the user's input. Here are a few of the buttons I am trying to sort through as well as the surrounding HTML.
<p class="checkbox-size">
<input type="radio" value="500" id="super_attribute[150]_500" name="super_attribute[150]" class="product_attribute">
<label for="super_attribute[150]_500">
<span id="label_eu0" class="label_hidden"> 38.5</span>
<span id="label_us0" class="label_show"> 6</span>
<span id="label_uk0" class="label_hidden"> 5.5</span>
<span id="label_cm0" class="label_hidden"> 24</span>
</label>
</p>
The specific button I am trying to press is this one:
<label for="super_attribute[150]_137">
<span id="label_eu10" class="label_hidden"> 45</span>
<span id="label_us10" class="label_show"> 11</span>
<span id="label_uk10" class="label_hidden"> 10</span>
<span id="label_cm10" class="label_hidden"> 29</span>
</label>
Now I tried many different methods of searching then clicking one of the buttons but nothing has worked yet. Any suggestions? Here is what I am currently using to try and find and click the button:
driver.find_element_by_css_selector("input[type='radio'][value='11']").click()
Looking back I might have not provided the correct code for the buttons, so here is a snapshot of the inspect element as well as the actual page if you want to go check it out for yourself. I am trying to click the size buttons. Button inspect element
Solved!
Here is the code I ended up using.
sizes = driver.find_elements_by_class_name('checkbox-size')
for size in sizes:
if size.text in [usersize]:
size.click()
print colored('Carted size %s'%(size.text), 'green')
break
continue
There are a few problems with your approach.
You are trying to click the INPUT when you need to click a SPAN
The sizes on the page contain some amount of whitespace which makes your strict string comparison, value='11', not work. Also the contained text is not considered a value.
Another issue you will run into is that all of the sizes aren't displayed. The sizes displayed is controlled by the links above the sizes, EU, US, UK, and CM. The ones that are displayed have class="label_show" so you will want to specify that in your answer or you will attempt to click elements that are not visible which will throw an exception. (Selenium only interacts with visible elements by design).
With all this, we can build the following XPath
//span[#class='label_show'][normalize-space(.)='10']
If it were me, I would throw this in a function that pass in the desired size as a string and insert that parameter into the above XPath to make this much more reusable.
Try the below locator(xpath):
.//span[contains(#id,'label_eu') and text()=' 40']
Ok so here is what I ended up using which solved the issue. I made a list of all the web elements with the class 'checkbox-size' then traversed that list to to find the correct size, and then click on that element.
Here is the code:
sizes = driver.find_elements_by_class_name('checkbox-size')
for size in sizes:
if size.text in [usersize]:
size.click()
print colored('Carted size %s'%(size.text), 'green')
break
continue
I am using Selenium PhantomJS to perform headless dynamic scraping. I was able to extract all information except popups triggered by an ng-click, such as:
<button href="#" ng-click="navigation.login({edu:false})">login</button>
<a class="btn btn-primary" ng-click="login()">signup</a>
I want to get the tag that contains ng-click label, so that I can perform onclick activity and extract information from it.
The ng-click value and tag can be anything, I just want to search whether a tag contains ng-click or not, and if it is then return that tag.
I don't want to use regex or something like that.
The most simple solution is using XPath to check length of value of ng-click.
elements = driver.find_elements_by_xpath("//*[string-length(#ng-click) > 1]")
for element in elements:
element.click()
It works.
elements = driver.find_elements_by_xpath("//*[(#ng-click)]")