Selecting dynamic element using selenium and python - python

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'.

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.

Can't dfferentiate between two selectors meant to perform one specific action

I've written a script in python in combination with selenium to click on a button named as follow located in a webpage. The thing is when I try with two different selectors, they both can click on the same button.
First selector:
"a[href$='/follow']"
Second one:
"a[href$='/follow'] > button"
Portion of relevant html:
<div class="nZSzR">
<h1 class="fDxYl">
some royal personality
</h1>
<span class="VerifiedBadge" title="Verified">
Verified
</span>
<a class="BY3EC" href="/accounts/follow" rel="nofollow">
<button class="L3NKy" type="button">
Follow
</button>
</a>
</div>
Which way I should stick to and why?
MITHU,
The css selector you have a[href$='/follow'], represent an anchor tag with href attribute that ends with /follow
second one a[href$='/follow'] > button , targets a button whose parent is anchor tag.
So, you can clearly see in second css, there is a dependency of a button. So, if you can go ahead with just first you should give priority to that.
For this HTML :
<a class="BY3EC" href="/accounts/follow" rel="nofollow">
<button class="L3NKy" type="button">
Follow
</button>
</a>
Clearly first css has less dependency.
If it's anchor tag then answer is no need of css selector.
You can directly go ahead with LINK_TEXT or PARTIAL_LINK_TEXT
Sample code :
continue_link = driver.find_element_by_link_text('Continue')
continue_link = driver.find_element_by_partial_link_text('Conti')
For more go through this official link.
So the > is a child combinator meaning you will only match the right hand side if it is a child of the left hand side.
Depending on how consistent pages are I would look for a faster selector and in this case go with a third option of
#react-root a
This is using your link: https://www.instagram.com/cristiano/?hl=en
An id and type selector will be faster than especially when using find_element_by_css_selector as you only need first match

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

Python xpath - Exclude content with style="display:none"

I'm using xpath to get some information from a website and I came across a block of code that contains style="display:none or block and I want to only include the code that has display:block; I watched some examples but I couldn't get it working on my code. I want to use an if statement to run the code if it has display:block but I don't know if that is possible. This is what I have:
if guide_page.xpath(".//div[#class='build-box']/#style/text()") == "display: block;":
for build_names in guide_page.xpath(".//div[#class='build-gradient']"):
for title in build_names.xpath("div/h2/text()"):
print("\n")
print(title)
And this is the div that has it:
<div class="build-box" style="display: block;">
I'm not sure if I should paste more of the html or if that's enough, otherwise, please tell me and thanks for any help :)
You can do this without using if statement. Just add a not(...condition...) in predicate to exclude elements matching certain condition. For example, the following XPath returns div elements with certain class attribute value, that don't have attribute style="display: block;" :
.//div[#class='build-box' and not(#style='display: block;')]

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.

Categories