Not able to select element using XPath - xpath

<div data-v-1dac319c="" class="ca-modal-header">
<span data-v-1dac319c="" class="ca-modal-header-title">New Claim Attachment</span>
<button data-v-1dac319c="" class="ca-modal-close material-icons">close </button>
</div>
I am unable to select the Close button using XPath.
Following is not working - //span[text()='New Claim Attachment']/following-sibling::button[text()='close']

Your XPath expression doesn't work because you have a small space at the end of your close . contains should work:
//span[text()='New Claim Attachment']/following-sibling::button[contains(., 'close')]

You can use this XPath:
//span[text()='New Claim Attachment']/..//button[contains(text(),'close')]
Or this:
//div[.//span[text()='New Claim Attachment']]//button[contains,text()'close')]

Related

To compare selenium xpath values

You are trying to run xpath values by comparing them.
You want to compare whether there are comments or not.
<div class="media-body">
<a href="https://url" class="ellipsis">
<span class="pull-right count orangered">
+26 </span>
post title </a>
<div class="media-info ellipsis">
admin <i class="fa fa-clock-o"></i> date </div>
</div>
If there is a comment, span class="full-right count or changed" is generated. If you don't have it, it won't be produced.
xpath comment //*[#id="thema_wrapper"]/div[3]/div/div/div[3]/div/div[7]/div[2]/div[1]/div[2]/a/span
xpath nocomment //*[#id="thema_wrapper"]/div[3]/div/div/div[3]/div/div[7]/div[2]/div[1]/div[2]/a/
I think we can compare this with if,else,but I don't know how.
if
#nocomment start
else
#comment stop
I searched a lot for the data, but I couldn't find it. Please help me.
Here's an XPath example to select/click on something without comment. This website seems to use the same system as your sample data :
http://cineaste.co.kr/
To select the entries with no comment for the movies block ("영화이야기"), just use :
//h3[.="영화이야기"]/following::div[#class="widget-small-box"][1]//li[#class="ellipsis"][not(contains(.,"+"))]
We verify the presence of the "+" in the li node to filter the data.
Oh, it's the same system. I tested it and there was an error.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//h3[.='영화이야기']/following::div[#class='widget-small-box'][1]//li[#class='ellipsis'][not(contains(.,'+'))]"}
(Session info: chrome=81.0.4044.138)
from selenium import webdriver
import time
path = "C:\chromed\chromedriver.exe"
driver = webdriver.Chrome(path) #path
'''
'''
driver.get("http://cineaste.co.kr/") #url
time.sleep(0.5)
postclick = driver.find_element_by_xpath("//h3[.='영화이야기']/following::div[#class='widget-small-box'][1]//li[#class='ellipsis'][not(contains(.,'+'))]") #로그인창 활성화
postclick.click()
driver.close()
Could you make an example with the site? I want to ignore the posts with comments and just click the ones without comments.

check condition to extract value from html response in jmeter

I'm new in JMeter, I'm working on one project on JMeter. where i have to extract value from HTML response depend on condition.
Actual data:
<li class="size size-item 159103">
<button id="productSizesAndGrid-btn-size-1" class="btn selection-button lowStock" data-skucode="159103" data-size="XS 8/10 " data-in-wishlist="false" data-stock-status-name="Low stock"> XS 8/10 </button>
</li>
I want to extract data-code but if data-stock="In stock".
data-stock can be out of stock as well so if data In Stock then and then only i want to extract that value.
I understand simple value extraction using regular expression extractor but couldn't understand how to do this.
when i working with this i realised there are disable data with same attribute and class
<li class="size size-item 159108">
<button id="productSizesAndGrid-btn-size-1" class="btn selection-button outOfStock disabled" data-skucode="159108" data-size="XS 8/10 " data-in-wishlist="false" data-stock-status-name="Out of stock"> XS 8/10 </button>
</li>
Edit:
I tried using below answer : it's not working in my case
Regular Expression : <button id="btnid" class="btn inStock" data-code="(.*?)"(.*?)data-stock="In stock">
Select the first template $1$
Using css selector:
#btnid
this works in my case now problem is i got two elements because both ids are same how to get specific element; i want second one as first one is disabled.
<button id="product-1" class="btn outOfStock disabled" data-code="123456" wishlist="false" data-stock="Out of stock" style="" css="1"></button>
<button id="product-1" class="btn inStock" data-code="123123" data-size="XS 8/10 " wishlist="false" data-stock="Low stock" style="" css="2"></button>
It works if you add 2 in Match No: 2
but i don't know it's correct way or not!
by above solution i still didn't get actual elements which are in stock if i get that then my problem solved but i have only temporary solution; i will appreciate if anyone can show me right way to do it.
Answer: below accepted answer work for me:
Match count: 13
Match[1]=
Match[2]=159109
Match[3]=159110
Match[4]=159111
Match[5]=159112
Match[6]=159103
Match[7]=159104
Match[8]=159105
Match[9]=159106
Match[10]=159107
Match[11]=
Match[12]=
Match[13]=
Don't use regular expressions for parsing HTML, they're hard to develop and maintain, moreover they're fragile and sensitive to markup changes.
I would recommend going for CSS Selector Extractor, the relevant CSS selector to match <button> tags where data-stock attribute is not Out of stock would be something like:
button:not([data-stock=Out of stock])
You need the data-code attribute
Demo:
To achieve this follow below steps:
Add Regular Expression Extractor as a child of the Sampler returning the Response
Regular Expression extractor setting as below:
Regular Expression : <button id="btnid" class="btn inStock" data-code="(.*?)"(.*?)data-stock="In stock">
Select the first template $1$
Proof of it working
Afterwards you can use ${dataCode} anywhere in the script.

How to write xpath to select item that doesnot have aria-hidden="true"?

I need to click "Cancel" button. So I tried writing an x-path that looks something like this :
//button[text()='Cancel']
But two elements are identified since two elements match the conditions as below, though one of the element is hidden :
<button type="button" class="gwt-Button button_action_id_9135994370013188418_9135995360013188733_compositepopup_3 TableCtrl-button cancel-button">Cancel</button>
<button type="button" class="gwt-Button button_action_id_9149469526113774095_9149539697213772969 TableCtrl-button cancel-button" aria-hidden="true" style="display: none;">Cancel</button>
Is there a way I could identify the element that does not have a tag like 'aria-hidden' ?
Try to use below XPath to match required (not hidden button):
//button[text()='Cancel' and not(#aria-hidden='true')]

How to write dynamic xpath for a webElement?

I have gone through from a lot of question on stackoverflow but didn't find the solution for my problem
-There is an element on webpage which has text= "Success"
Now I want to locate that element, id of the elements keeps changing. There are some other elements with a similar name say - successful, unsuccessful, successfully etc.. on the same page but I want to search the particular element "Success". How should I do it? In this case,
I don't think- startsWith() and contains() methods of XPath will work.
You can use the following xpath to find it. i) //[text()='Success'] or ii) //[.='Success'].
You can always use other attributes. You are not limited to id.
A few examples below:
Basic xpath syntax:
//tag[#tag-attribute='arrtibute-value']
//*[contains(#tag-attribute,'attribute-value')]
//tag[text()='inner-text']
//*[contains(text(),'inner-text')]
Where:
tag = the html tag
* = tag wildcard - matches any tag
tag-attribute = any attributes within the tag (name,id,class...)
attribute-value = the value of the specified tag-attribute
Consider this sample:
<div id="countryDiv1">
<button type="button" class="btn btn-default" data-toggle="dropdown" data-id="ConsignorAddressCountryId" tabindex="1" id="randomGenerated">Text1</button>
</div>
<div id="countryDiv2">
<button type="button" class="btn btn-default" data-toggle="dropdown" data-id="ConsignorAddressCountryId" tabindex="2" id="randomGenerated">Text2</button>
</div>
Here we have 2 buttons having the same random generated IDs but a few differences like tabindex, inner text, id of parent div.
Sample xpath(s) for button 1:
//button[#tabindex='1']
//button[#data-id='ConsignorAddressCountryId'][#tabindex='1']
//button[#data-id='ConsignorAddressCountryId'][text()='Text1']
//div[#id='countryDiv1']//button
Personally, I prefer using other attributes instead of text mainly because texts can have reserved characters that might mess up your xpath.
You can also check your xpath validity using the following:
Chrome Dev Tools (F12) > Elements Tab > Hit CTRL+F while under elements tab > paste your xpath > valid elements will be highlighted
Chrome Dev Tools (F12) > Console Tab > Enter: $x("yourxpath") > hit enter key > matching elements will be displayed on the console.
You can try following:-
//*[#text()='Success']

how to access this element

I am using Watir to write some tests for a web application. I need to get the text 'Bishop' from the HTML below but can't figure out how to do it.
<div id="dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view" style="display: block;">
<div class="workprolabel wpFieldLabel">
<span title="Please select a courtesy title from the list.">Title</span> <span class="validationIndicator wpValidationText"></span>
</div>
<span class="wpFieldViewContent" id="dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view_value"><p class="wpFieldValue ">Bishop</p></span>
</div>
Firebug tells me the xpath is:
html/body/form/div[5]/div[6]/div[2]/div[2]/div/div/span/span/div[2]/div[4]/div[1]/span[1]/div[2]/span/p/text()
but I cant format the element_by_xpath to pick it up.
You should be able to access the paragraph right away if it's unique:
my_p = browser.p(:class, "wpFieldValue ")
my_text = my_p.text
See HTML Elements Supported by Watir
Try
//span[#id='dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5b45385e5f45b_view_value']//text()
EDIT:
Maybe this will work
path = "//span[#id='dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5b45385e5f45b_view_value']/p";
ie.element_by_xpath(path).text
And check if the span's id is constant
Maybe you have an extra space in the end of the name?
<p class="wpFieldValue ">
Try one of these (worked for me, please notice trailing space after wpFieldValue in the first example):
browser.p(:class => "wpFieldValue ").text
#=> "Bishop"
browser.span(:id => "dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view_value").text
#=> "Bishop"
It seems in run time THE DIV style changing NONE to BLOCK.
So in this case we need to collect the text (Entire source or DIV Source) and will collect the value from the text
For Example :
text=ie.text
particular_div=text.scan(%r{div id="dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view" style="display: block;(.*)</span></div>}im).flatten.to_s
particular_div.scan(%r{ <p class="wpFieldValue ">(.*)</p> }im).flatten.to_s
The above code is the sample one will solve your problem.

Resources