Get XPath of parent - python-2.7

here's code:
<div class="x-form-item " tabindex="-1" role="presentation">
<label class="x-form-item-label" style="width:180px;" for="x-auto-4005-input">Amount split option:</label>
<div id="x-form-el-x-auto-4005" class="x-form-element x-form-el-x-auto-4005" style="padding-left:185px" role="presentation">
<div id="x-auto-4005" class=" x-form-field-wrap x-component x-trigger-wrap-focus" role="combobox" style="width: 240px;">
I would like to access element with id "x-auto-4005", but without using this id. I can see that label element has identifier "Amount split option". How can I access my element using the label? I guess I would have to get XPATH of parent of all the elements?

You can try the following xpath:
"//label[text()='Amount split option:']/following::div[2]"

Related

Extract information from all matching nodes without looping xpath

<ul class="products-grid">
<li class="item">
<div class="product-block">
<div class="product-block-inner">
<img src="#/producta.jpg">
<h2 class="product-name">Product A</h2>
<div class="price-box">
<span class="regular-price" id="#">
<span class="price">Rs 1,849</span>
</span>
</div>
</div>
</div>
</li>
<li class="item">
<div class="product-block">
<div class="product-block-inner">
<img src="#/productb.jpg">
<h2 class="product-name">Product B</h2>
<div class="price-box">
<span class="regular-price" id="#">
<span class="price">Rs 1,849</span>
</span>
</div>
</div>
</div>
</li>
</ul>
I am at this moment scraping the item in a loop.
products = response.xpath('//ul[#class="products-grid"]//li//div[#class="product-block"]//div[#class="product-block-inner"]').extract()
After getting the product-block-inner node, I save it into products and then I will have to loop like
for product in products:
// parse the div.product-block-inner further deep down
// to get name, price, image etc
// and save it to a dict and yeild
pass
Is this possible that i get text, href for all div.product-block-inner in the final list without looping
Yes, but it's very confusing, for example you could try this:
products = response.xpath(
'//ul[#class="products-grid"]//li//div[#class="product-block"]//div[#class="product-block-inner"]'
).css(
'.product-name a::attr(href), .product-name a::text, .price::text'
).extract()
but I would suggest to always loop (btw, why do you call extract() when you assign it to products?)
products = response.xpath(
'//ul[#class="products-grid"]//li//div[#class="product-block"]//div[#class="product-block-inner"]'
)
for product in products:
yield {'name': product.css('.product-name a::text').extract_first()
'url': product.css('.product-name a::attr(href)').extract_first()
'price': product.css('.price::text').extract_first()}
(I've used css selectors in this case because the equivalent xpaths are longer, but the same can also be achieved using xpath)

How to get only text value from the below <li> in dropdown (required XPATH)

Questions:
Currently I am using below xpath to get the text under li ul
//*#id=['Merchantserviceproviders_country_chosen']/div/ul/li
however Output as follows:
ChromeDriver: chrome on XP (6624880cd868883159df5e5e631e1e4f)]
Expected:
United States (text mentioned under li)
Please help here to identified the correct XPATH?
<div class="chosen-container chosen-container-single chosen-container-active" style="width: inherit;" title="" id="Merchantserviceproviders_country_chosen">
<a tabindex="-1" class="chosen-single"><span>Select</span><div><b></b></div></a>
<div class="chosen-drop"><div class="chosen-search"><input type="text" autocomplete="off"></div>
<ul class="chosen-results">
<li class="active-result" style="" data-option-array-index="0">Please select country</li>
<li class="active-result" style="" data-option-array-index="1">United States</li>
<li class="active-result" style="" data-option-array-index="2">Canada</li>
<li class="active-result" style="" data-option-array-index="3">United Kingdom</li>
<li class="active-result" style="" data-option-array-index="4">Ireland</li>
<li class="active-result" style="" data-option-array-index="5">South Africa</li>
</ui>
Your xpath will return a list of li webelements. Iterate the list to get the text with getText method. If you want just USA one append [.='United States'] to your xpath
(Assuming you are using Java) You should try using By.cssSelector() as below :-
driver.findElement(By.cssSelector("li.active-result[data-option-array-index='1']")).getText();
Give it a try by adding [2] after 'li' in xpath (/li[2]).

Selenium Python XPATH Can i print the value of an html tag id attribute to the console

I have an HTML with a div tag which has an ID attribute. The ID value is dynamic.
I have managed to build an XPATH to locate the ID attribute and get's it's value into a variable.
Can i print out the value to the console? I want to know what value the variable has got.
I tried to print the value the following but i get errors:
I tried:
print id.text
print id.get_attribute("id")
The errors are:
AttributeError: 'unicode' object has no attribute 'get_attribute'
AttributeError: 'unicode' object has no attribute 'text'
The Selenium Python code is:
element = self.driver.find_element(By.XPATH, '//*[starts-with(#id,"operations_add_process_list_task")]//span//button')
id = element.get_attribute("id")
print id.text
The XPATH is:
(By.XPATH, '//*[starts-with(#id,"operations_add_process_list_task")]//span//button')
The HTML is:
<div id="operations_add_process_list_task_2">
<span/>
<span>
<span class="myinlineblock" title="Clean"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;">
<select tabindex="-1">
</span>
</span>
<span>
<span class="" title="Turn group off or on." style="">
<input type="checkbox" checked="" tabindex="-1"/>
</span>
</span>
<span>
<button class="gwt-Button" title="Add the tasks to the selected group." style="display:block;" type="button">Add tasks
</button>
</span>
</div>
Once I know what the value is I can then use the ID for the next locator e.g.
select = Select(WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//div[#id="%s"]/span[2]//select' % id))))
Thanks,
Riaz
Alternatively, you can change the XPath to be as shown below to return the element with id that also contains the button element :
//*[starts-with(#id,"operations_add_process_list_task")][.//span//button]
Or you might want to use child axes instead of // if the span is direct child of the element with the target id, because the former will be slightly more efficient :
//*[starts-with(#id,"operations_add_process_list_task")][span/button]

Selenium Python Xpath how to select the correct span text from many nested div tags

I have a web page with a left hand menu. It is made up of many div tags.
I have noticed when my Selenium Python script runs it is not clicking the text I want clicked from the left hand menu. It is clicking something else.
My Xpath is not correct.
I would like to locate the text "Statistics" (it is in a div\span tag) which has the parent div text "Analysis"
It is not clicking the correct text "Statistics" because there maybe another "Statistics" somewhere in the HTML source. If i start from the div tag which has the text "Analysis" and then find the text "Statistics" then I will get the correct element.
My Xpath is:
.//div//span[#title="Analysis"]/following::div[5]//span[text()="Statistics"]
The HTML is:
<div>
<span class="" title="Analysis"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;">Analysis</span>
</div>
</div>
</div>
</div>
</div>
<div style="overflow: hidden;">
<div>
<div>
<div aria-selected="false" role="treeitem" aria-setsize="3" aria-posinset="1" aria-expanded="false"
aria-level="2">
<div class="GJPPK2LBIF" style="padding-left: 16px;">
<div class="GJPPK2LBIF GJPPK2LBKF" style="padding-left: 16px;position:relative;" onclick="">
<div class="GJPPK2LBJF" style="left: 0px;width: 15px;height: 15px;position:absolute;">
<img border="0"
style="width:15px;height:15px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA7/HbnjJn53wAAAABJRU5ErkJggg==) no-repeat 0px 0px;"
src="http://test1:8080/clearcore/ClearCore/clear.cache.gif"
onload="this.__gwtLastUnhandledEvent=" load";"/>
</div>
<div class="GJPPK2LBLF">
<div style="padding-left: 22px;position:relative;zoom:1;">
<div style="left:0px;margin-top:-8px;position:absolute;top:50%;line-height:0px;">
<img border="0"
style="width:16px;height:16px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaekJggg==) no-repeat 0px 0px;"
src="http://test1:8080/clearcore/ClearCore/clear.cache.gif"
onload="this.__gwtLastUnhandledEvent=" load";"/>
</div>
<div>
<span class="" title="Statistics"
style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;">Statistics</span>
</div>
</div>
</div>
</div>
</div>
</div>
Thanks,
Riaz
If you have FireFox with FirePath you can test the xpath and see how many and which matches you get. For instance:
//span[text()="Statistics"]
This may result in 1 matching node but also in more. Let's assume there's two matches and the one you want is the second one. Then you'd choose:
//span[text()="Statistics"][2]

Display element in form using Zurb Foundation

I'm using the CSS Framework Foundation and when I write the following code:
<div class="row">
<div class="two mobile-one columns"><label for="invoiceterm-summary" class="right inline">Become late <span class="required">*</span></label></div>
<div class="ten mobile-three columns"><input class="one" id="invoiceterm-late" name="invoiceterm[late]" type="text" value=""> days after invoice sent</div>
</div>
​
I get this :
But I want to display the text "days after invoice sent" like this:
You should place all of these elements into a single:
<div class="row">
<div class="twelve mobile-four columns"></div>
</div>
Otherwise it is behaving correctly, stacking the columns as the screen size is reduced.
I had to remove the display: block property from the css definition of the input[type="text"] on the foundation.css file