Couldn't click on menu link [duplicate] - selenium

This question already has answers here:
How to add explicit wait in PageFactory in PageObjectModel?
(2 answers)
Closed 3 years ago.
I'm trying to open a menu in a list of menus' and then click on a link in the menu. the menu list structure is like there are two links side by side. one link goes to the main section that gives the summary and the other one opens the menu. I can click on the link that goes to the main section but couldn't click on the link that opens the menu though the XPath I've used can uniquely identify the menu link. The HTML is :
<li _ngcontent-c9="" class="list-group-item d-flex justify-content-between align-items-center">
<i _ngcontent-c9="">...</i>
<div _ngcontent-c9="" class="center" style="font-family: ITC Avant Garde Gothic Std Book;font-size: 20px;">
<a _ngcontent-c9="" routerlink="../someText/mainlink" href="#/someText/mainlink">Main Link</a>
<span _ngcontent-c9="" style="padding-left:40%">
<a _ngcontent-c9="" class="fa fa-bars" data-target="#basicExample" data-toggle="modal" placement="left">
</a>
</span>
</div>
</li>
I need to get to the link under the span.
There is no text Menu Link. I've added that text so that the structure can be understood. The XPath I've used for the menu link:
.//a[./text()='Main Link']/../span/a
The XPath I've used for the main link is:
.//a[./text()='Main Link']
Also, I've defined that element using the page factory as:
#FindBy(linkText='Main Link')
WebElement mainLink;
Both are working when I try to click on the main link. but I couldn't click on the menu link beside it. I've implemented an explicit wait for 20 seconds. I'm getting TimeOutException.

You can use any of the below xpaths.
Option 1:
//a[.='Main Link']/following-sibling::span/a
Option 2:
//a[#data-target='#basicExample']

Related

Finding an element with a xpath that changes

Hello I am new to selenium and trying to find an element on a dropdown that expands to a list. The xpath constantly changes so cannot use it. Also tried the full xpath and did not work either. Other drop downs that I have used I have been able to use the cssselector but really do not have much to work with in here. I have tried with 'Account Maintenance' text but it did not work or did not do it properly. If I need to provide any additional information or more html please let me know.
<li class="k-item" role="treeitem" data-uid="706aa3e9-cd95-45c3-9ad0-e990887f9484" aria- selected="true" aria-expanded="false">
<div class="k-mid">
<span class="k-icon k-i-expand">
::before
</span>
<span class="k-in k-state-selected">Account Maintenance</span>
</div>
<ul "THIS IS THE LIST THAT EXPANDS AFTER CLICKING ON THE 'Acount Maintenance' text above but not my issue </ul>
</li>

How to click on an element in a list in robot framework

How can i select settings from this dropdown list.
I tried Click Element by(class/id..) but none of them work for me.
First i have to click on the profile icon, then choose the settings element
here is the html code of the icon :
<li class="dropdown userMenu hidden-sm hidden-xs" id="y7">
<a data-toggle="dropdown" class="dropdown-toggle pad10-top no-padding-left no-padding-right center" href="javascript:void(0)" id="yu">
<span class="ellipsis-menu no-margin pad5-bottom" data-toggle="tooltip" data-placement="bottom" title="Profile" id="yui_3_">
<i aria-hidden="true" class=" icon-user_icon pad5-right" id="y"></i>
</span>
<b class="caret"></b>
</a>
and here is the element of the list that i want to select :
<li id="y8">
<a id="settings" href="/cc/settings.html"><i class=""></i>Settings</a></li>
Any help please?
This should work.
Assuming that the list line element is of profile icon (It was unclear to me from the HTML you pasted, but you may better figure out on your application by inspecting it).
If needed be, you may apply a Sleep of 1-2 sec in between the two lines
Click Element xpath: //*[#title='Profile']
Click Element id: settings
P.S. I am assuming that you have imported all the required libraries for this (in fact, it is only SeleniumLibrary that is required for these two lines)

Unable to locate the element after clicking on toggle button

I am trying to automate the assignment for toggle button on the below link :
http://way2automation.com/way2auto_jquery/accordion.php
where the icons will go on and off based on the toggle button.
The HTML code snippet for the buttons are :
<h3 class="ui-accordion-header ui-state-default ui-accordion-icons ui-corner-all" role="tab" id="ui-id-1" aria-controls="ui-id-2" aria-selected="false" aria-expanded="false" tabindex="-1"><span class="ui-accordion-header-icon ui-icon ui-icon-circle-arrow-e"></span>Section 1</h3>
I have switched to the frame already and trying to check the element using :
chromeDriver.findElement(By.xpath("//*[#id=\"ui-id-1\"]/span")).isEnabled()
however after clicking on toggel buttons the icon gets disappeared and the element is not visible at all. Hence I am getting error for the above line. Is there any way to check the icons are there or not?

Selenium XPATH for dynamic class for dropdown

I am new to selenium and I am having trouble to write an xpath selector for this dropdown element:
<li style="" class="dropdown open" data-bind="visible: currentServers().length > 0">
<a id="employeeList" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true" data-bind="text: currentServerName()">employee servers</a>
<ul role="menu" class="dropdown-menu">
I have tried various xpath and it was not able to click on the dropdown.
Can any one help me with the xpath? I have used xpath with id , class, cssselector . But none of them worked.
If the root issue is the li element changes its class when opened - as stated in the comments, then the solution would be not to go after an exact match in it, but rather containing the looked for string. I.e.:
//li[contains(#class, "dropdown")]
Prepend/append any other elements as needed (e.g. an a element beneath it, if it is the one to relieve the click, etc)
Try to do this way.
Explanation: Your html inside the li tag, so start your xpath with li tag and then move ahead with a tag along with text method.
//li/a[contains(text(), 'employee servers')]

how to click on a button when specific image is asscoiated with it

I am using selenium for testing my application.
In my application there are 5 buttons, each have a different image associated with it.
I want to click on button which have a specific image associated.
Currently i am using a while loop to get the node of image and then replacing this node into xpath of button to select it.
is there any way either with xpath or css to do this directly.
Providing more information-this is like submit button is there and then below this image is there. submit button and images are sibling element and need to click submit button when the next element is specific image
<div class="select">
<span class="sysTxtBtn submit xxs">
<span class="btnTagDummy">
</span>
<div class="specialRateMarking">
<img width="79" height="11" alt="Marking2" src="someimages"/>
</div>
<div class="select">
<span class="sysTxtBtn submit xxs">
<span class="btnTagDummy">
</span>
<div class="specialRateMarking">
<img width="79" height="11" alt="Marking1" src="someimages"/>
</div>
Could you include a snippet of your HTML? Below is an example of an image in a form and a few ways of locating it using Selenium, but these may not be relevant depending on your implementation:
<input id="submitForm" name="imgbtn" type="image" src="images/submit.png" />
id=submitForm
name=imgbtn
//input[#src='images/submit.png']
//input[contains(#src, 'submit.png')]
css=input[src='images/submit.png']
UPDATE:
Given the HTML:
<div class="select">
<span class="submit">
<div class="marking1"></div>
<div class="select">
<span class="submit">
<div class="marking2"></div>
You can locate the 'submit' span parent of the 'marking2' div using the following XPaths:
//div[#class='marking2']/..
//div[#class='marking2']/parent::*
//div[#class='marking2']/parent::span
UPDATE 2:
Based on the HTML now included in the question, you can locate the span with the class of submit related to the image many ways, a few examples follow:
//div[//img[#alt='Marking2']/span[contains(#class, 'select')]
//img[#alt='Marking2']/../../span
//div[img[#alt='Marking2']]/preceding-sibling::span
I hope this gives you some ideas. I'd certainly recommend XPath over CSS for locating these elements as it's much better at these sorts of relationships.