Replace Menu in website using Xpath - xpath

I want to use Xpath to replace my website menu of odoo 8.
I don't want to replace all the header, but only one element in the menu : The shop
When i inspect the element li , i see this :
<span data-oe-model="website.menu" data-oe-id="5" data-oe-field="name" data-oe-type="char" data-oe-expression="submenu.name" data-oe-translate="1">Shop</span>
I want to replace this element by creating a mega menu only for "shop".
I don't know how to use Xpath for this case.
Any solution ? Thank's

<template id="any_new_id" inherit_id="module_name.id_of_template">
<xpath expr="//span" position="replace">
your mega menu code
</xpath>
</template>
if you have any unique identifier then you can use it like this expr="//span[#id='span_id']" otherwise it replace all span tags

Related

Not able to click a dropdown field which is designed in the DOJO Html using selenium

I am not able to click an arrow drop down filed in my application using selenium web driver.
I tried lot of XPath using class name and relative XPath
This is the code used for the problem
<span class="dijitReset dijitInline dijitIcon pentaho_dijitEditorIconExport"
data-dojo-attach-point="iconNode"></span>
Please add some more information from your HTML i just add some text and xpaths are like that
<span class="dijitReset dijitInline dijitIcon pentaho_dijitEditorIconExport"
data-dojo-attach-point="iconNode">test</span>
Xpaths are:
//span[#class='dijitReset dijitInline dijitIcon pentaho_dijitEditorIconExport']
or
//span[#data-dojo-attach-point='iconNode']
or
//span[#data-dojo-attach-point='iconNode' and #class='dijitReset dijitInline dijitIcon pentaho_dijitEditorIconExport']
Add some more informaton if you have any concern
dojo combo are basically <input type= "text">, once u click on it or type the first letter of the option you want to select, a <div> is attached to the html body which has following structure:-
<div resultname="option name" resultvalue="option value" class="dojoComboBoxItem dojoComboBoxItemEven ">Option Value</div>
now there are 3 steps to select from dojo
identify the input text
type the first few letters of the option you want to select
create dynamic xpath to select the option
the code goes as followes
String optionName = "Option You Want to Select";
WebElement dojoBox = driver.findElement(By.xpath("<provide the xpath here>"));
dojoBox.sendKeys(optionName.substring(0,2));
driver.findElement(By.xpath("//*[#id='page-home']/span/div[#resultvalue='" + optionName + "']")).click();
if you are not sure about the dynamic xpath structure, then manually select the option, and inspect the div you have added, generally it should have the similar structure.

identifying xpath from Ext JS application

I'm trying to automated a application designed in Ext Js, xpath identification is become complex. Please help in getting the xpath for the below scenario.
For a dropdown having a set of names, need to select a name from the list available.
I'm using a mouse action to locate the drop down name and then moveToElement particular element in the List of menu present.
For example If the menu is containing a list of names like, Abi, Ashwini Asha, Ashwini, Diva.
Using parameterizing I'm able to select the names from the menu.
But when I need to select Ashwini from the menu ,since Ashwini Asha is already present in the menu , Ashwini Asha object gets clicked.
contains() is not working here. Which function should I use?
The below is the code which am using :
html :
<div id="combo-1023-inputWrap" class="x-form-text-wrap x-form- text-wrap-default" role="presentation" data-ref="inputWrap">
<input id="combo-1023-inputEl" class="x-form-field x-form-text x-form-text-default " type="text" autocomplete="off" aria-autocomplete="list" aria-expanded="false" aria-haspopup="true" aria-required="false" aria-invalid="false" aria-readonly="false" aria-disabled="false" aria-hidden="false" role="combobox" value="Triton" name="selectedName" size="1" data-ref="inputEl" data-componentid="combo-1023">
......
<li>------------ Ashwini Asha</li>
<li>------------ Ashwini</li>
</div>
to click on the menu :
action.moveToElement(driver.findElement(By.xpath("//input[#name='selectedName']"))).click().perform();
to load the data in the menu :
wait.until(ExpectedConditions.visibiltyOfElementLocated(By.xpath("//li[contains(text(),'"+NameParameter+"')]")));
click the object :
action.moveToElement(driver.findElement(By.xpath("//li[contains(text(),'"+NameParameter+"')]"))).click().perform();
Let me know how to select the name Ashwini alone?
Thanks
No need to use contains in your xpath. Go for exact match of the name.
By.xpath("//li[text()='"+NameParameter+"']")
or
By.xpath("//li[.='"+NameParameter+"']")
If I'm understanding this correctly, you could use:
Driver.Instance.findElements(...
This would return an array in which you could just iterate and perform actions on based on the .text of the element.

How can i add the same attribute to all matched items of an odoo xpath query (not only to the first)?

I want add the same attribute to all matched items of an odoo xpath query inside a inherited qweb report template.
My code only change the colour of "1. aaa" to red. (the first matched item)
I can use "/*[#class='test_class'][2]" for the second item, but this is not a solution there are to many items with "test_class" inside the inherited report.
<template id="test_report_document_raw">
<h2 class="test_class">1. aaa</h2>
<h2 class="test_class">2. bbb</h2>
<h2 class="test_class">3. ccc</h2>
...
</template>
<template id="test_report_document" inherit_id="test.test_report_document_raw" primary="True">
<xpath expr="//*[#class='test_class']" position="attributes">
<attribute name="style">color: red;</attribute>
</xpath>
</template
AFAIK it is an Openerp bug which doesn't consider multiple xpath corrispondances.
In alternative you could override fields_view_get and use xpath through lxml library.
As you said you can use the xpath expressions "/*[#class='test_class'][2]","/*[#class='test_class'][3]",..,"/*[#class='test_class'][2]" and so on and update the style.
Other solution is to update the css class with your new style. So all where ever you are using this class 'test_class' there all this change will affect.

XPath is broken when changes are made

This is the current situation: There are several files and folders in a specific location.
Once you hover over a file or a folder, an icon appears then you can click on that icon and a menu will appear. From that menu a user can select any action(rename, move,etc). The problem is that the XPath which is provided by FirePath is broken whenever a new file/folder is created/added in the location.
This is the initial line of code which works fine until the XPath is broken:
webDriver.findElement(By.xpath("//*[#id='main_files_view']/ol/li[6]/ul/li[4]")).click();
Whenever a new item is added in the location, the index with value "6" can change to "7" (new position of the file) and the XPath generated is now slightly different:
webDriver.findElement(By.xpath("//*[#id='main_files_view']/ol/li[7]/ul/li[4]")).click();
How can I change that XPath and makes it robust so that no matter the number of items added/removed, the XPath will not break?
Below is the section of the HTML which is related to the XPath provided by FirePath.
When the XPath is provided, the last class is also highlighted.
<li class="storage_item document file_object even ui-draggable" data-thumb-translation="Translated" data-possible-actions="Rename Delete Share Move View" data-file-size="0 bytes" data-item-type="file" data-display-name="solids_A" data-name="solids_A.raas" data-id="bd48453c752043d98afb237b86ee88a3">
<a class="file_name" href="#/Item/Details?id=bd48453c752043d98afb237b86ee88a3&itemtype=File&tab=Default">
<img class="file_list_icon" width="16" height="16" src="https://api-staging.autodesk.com/content/gateway/2013.1.307595.626/z/Content/images/fileIcons/small/raas.png"/>
<div class="name_container">
<ul class="tools">
<li class="preview_trigger"/>
<li class="comment_balloon none has_tooltip" data-tooltip-contents="#comment_balloon_tooltip" data-comments="0">
<li class="categorize action has_tooltip" data-tooltip="Categories"/>
<li class="document_tools has_tooltip" data-tooltip="Actions"/>
</ul>
</li>
I am using Selenium 2.0, on Eclipse IDE.
If the data-name is unique, then you have
//*[#id='main_files_view']/ol/li[#data-name='solids_A.raas']/ul/li[contains(#class, 'document_tools')]
Css Selector is even better than XPath in this case:
#main_files_view li[data-name='solids_A.raas'] .document_tools
Try this:
By.XPath("//*[#id='main_files_view']/ol/li/ul/li[4]")
You do not have to define every index in an xpath and often if it's just one that change, removing that index will work.
In general you should avoid xpaths in your tests. Css selectors are faster (especially in IE) and more readable. If data-name is unique then this should work:
webDriver.findElement(By.cssSelector("#main_files_view li[data-name='solids_A.raas'] li.document_tools"));
If you really need an xpath the one provided by user1177636 will also do the job.

Click a button with XPath containing partial id and title in Selenium IDE

Using Selenium IDE, I'm trying to click a button within a table on a webpage using XPath with a partial id and a title from the element. The XPath I'm using is:
xpath=//*[contains(#id, 'ctl00_btnAircraftMapCell')]//*[contains(#title, 'Select Seat')]
and thats the entire html code for an example of the buttons im trying to click:
<li id="ctl00_MainContent_repAircraftMap_ctl20_repAircraftMapRow‌​_ctl00_liAircraftMap‌​Cell" class="">
<a id="ctl00_MainContent_repAircraftMap_ctl20_repAircraftMapRow‌​_ctl00_btnAircraftMa‌​pCell" href="javascript:void(0)" seatnumber="20A" mapbindattribute="1124" title="Select Seat 20A" onclick="SeatClick(1124);"></a>
</li>
Am I constructing this incorrectly? It's not working!
Now that you have provided your HTML sample, we're able to see that your XPath is slightly wrong. While it's valid XPath, it's logically wrong.
You've got:
//*[contains(#id, 'ctl00_btnAircraftMapCell')]//*[contains(#title, 'Select Seat')]
Which translates into:
Get me all the elements that have an ID that contains ctl00_btnAircraftMapCell. Out of these elements, get any child elements that have a title that contains Select Seat.
What you actually want is:
//a[contains(#id, 'ctl00_btnAircraftMapCell') and contains(#title, 'Select Seat')]
Which translates into:
Get me all the anchor elements that have both: an id that contains ctl00_btnAircraftMapCell and a title that contains Select Seat.

Resources