Opening a href link in Selenium Java - java

I am trying to click on the following a href link in selenium. I have tried using xpath and By.linkText() and By.cssSelectort() but I have not be able to locate the element. Any help is much appreciated
driver.findElement(By.linkText("CCC_PH3_Sandbox_Keybridge: CCC PH3 Sandbox Keybridge")).click();
and By.xpath() but its unable to locate the element
WebElement course = driver.findElementByXPath("html/body/table/tbody/tr/td/div/div[2]/table/tbody/tr/td[3]/a/span[1]");
course.click();
css locator :
driver.findElement(By.cssSelector("a[href*='/webapps/portal']")).click();
Here is the html snippet:
<img width="12" height="12" src="/images/ci/icons/bookopen_li.gif" alt="">
<a target="_top" href=" /webapps/portal/frameset.jsp?tab_tab_group_id=_2_1&url=%2Fwebapps%2Fblackboard%2Fexecute%2Flauncher%3Ftype%3DCourse%26id%3D_2135_1%26url%3D">CCC_PH3_Sandbox_Keybridge: CCC PH3 Sandbox Keybridge</a>

Try using By.partialLinkText() as below :-
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("CCC_PH3_Sandbox_Keybridge"))).click();
Edited:- As you have mentioned this element is inside two iframes with id navFrame and contentFrame, you need to switch that iframes one by one before finding element as :-
driver.switchTo().frame("navFrame");
driver.switchTo().frame("contentFrame");
//Now find desire element
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("CCC_PH3_Sandbox_Keybridge"))).click();
//After doing all stuff inside iframe switch back to default content
driver.switchTo().defaultContent();

Related

xPath Text Contains Selenium Web Driver

I'm trying to select an element based on its text contents. I am using XPath to achieve this.
I am just puzzled as this should work?
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
I'll even copy the HTML code:
<div class="linkWrap noCount">Notifications <span class="count _5wk0 hidden_elem uiSideNavCountText">(<span class="countValue fsm">0</span><span class="maxCountIndicator"></span>)</span></div>
The div element has the words "Notifications" inside it. So why doesn't it work.
Go to this page on Facebook: https://www.facebook.com/settings
Use this chrome extension to highlight any area via xPath.
You have a space before the word Notifications:
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
You should also add a wait for element before trying to find the element:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(), 'Notifications')]"));
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
I found the issue with the help of some amazing people in this community.
Ok, so my element was in an iFrame.
In order to access the element, I must first access the iFrame
WebElement iframe = obj.driver.findElement(By.xpath("//iframe[#tabindex='-1']"));
obj.driver.switchTo().frame(iframe);

How to switch to the iframe as per the HTML provided through Selenium and Java?

This way I tried to switch to the frame:
driver.switchTo().frame(driver.findElement(By.xpath(("//iframe[contains(#name,'vfFrameId')]//following::iframe[2]"))));
There are three iframes on the complete html code and I am trying to switch to the third one and I am able to the locate that as well using xpath but after running the script driver is not able to locate that and throwing an exception.
Exception:
no such element: Unable to locate element: {"method":"xpath","selector":"//iframe[contains(#name,'vfFrameId')]//following::iframe[2]"}
HTML:
<div class="windowViewMode-normal oneContent active forcePageHost" data-aura-rendered-by="1330:0" data-aura-class="forcePageHost">
<div class="oneAlohaPage" data-aura-rendered-by="1335:0" data-aura-class="oneAlohaPage">
<force-aloha-page data-data-rendering-service-uid="240" data-aura-rendered-by="1338:0" force-aloha-page_aloha-page="">
<div class="slds-template_iframe slds-card" force-aloha-page_aloha-page="">
<iframe height="100%" width="100%" scrolling="yes" allowtransparency="true" id="vfFrameId_1536141890078" name="vfFrameId_1536141890078" allowfullscreen="true" force-aloha-page_aloha-page="" allow="geolocation *; microphone *; camera *" title="Deploy Data Set"></iframe>
</div>
</force-aloha-page>
</div>
</div>
Here is how you can find the index of each iframe element
List <WebElement> options= driver.findElements(By.tagName("iframe"));
for(WebElement e: options) {
String index=e.getAttribute("index");
System.out.println("Index for: "+e.getAttribute("name")+" is: "+index);// to print out name attribute and index
}
If you are switching from iframe to iframe that don't work all the times. You may need to switch to default content from iframe(1) first and then switch to iframe(2)
driver.switchTo().frame(your locator for 1st iframe here);// to switch to 1st iframe
// your action here
driver.switchTo().defaultContent();
driver.switchTo().frame(your locator for 2nd iframe here); // to switch to 2nd iframe
I am working from your comment:
so you can see the code above where you can see that there are three iframes available on the browser. what happens that i click on one link then to interact with the elements i have to switch to the iframe and here i am able to switch but after clicking on an element ,i am getting navigated to the next page and there is another iframe introduced where i am not able to locate the iframe
You need the following sequence of events:
Switch to your iframe. From this point, your driver instance is pointing at this iframe!
Click on the element that transitions to the next page.
Your driver instance is still pointing at the iframe from step 1! You need to reset it to the top document using: driver.switchTo().defaultContent();.
Only now can you switch to the new iframe on the page you just navigated to.
As per the text based HTML and the HTML snapshot you have shared, it is not evident if there are multiple iframes available. So considering this <iframe> as a unique iframe you need to induce WebDriverWait while switching to the <iframe> as follows:
cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id^='vfFrameId_'][name^='vfFrameId_'][title='Deploy Data Set']")));
xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#id,'vfFrameId_')][starts-with(#name,'vfFrameId_')][#title='Deploy Data Set']")));
Incase there are multiple iframes available with the same attributes which we have used above you can use the following solution:
xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//div[#class='windowViewMode-normal oneContent active forcePageHost' and #data-aura-class='forcePageHost']//following::iframe[1]")));

Unable to locate element using Selenium Webdriver

I am having sticky nav bar which has few list elements where each contains href element. When I tried to locate element I am getting the error.
Following is my HTML code :
<div class="nav" id="sticky">
<div class="container">
<ul class="main-nav">
<li>Dashboard</li>
<li><a href="../MEFAcademicDash/StudentUI/StudentHome.aspx"
title="Academic Dashboard">Academic</a></li>
<li>Notices</li>
I want to locate Academic through Webdriver, I am getting the error like this Unable to locate the element.
org.openqa.selenium.NoSuchElementException: Unable to locate element: /html/body/div/form/div[5]/div[1]/ul/li[2]/a
As per the HTML you have shared you need to induce WebDriverWait for the element to be visible / clickable. To click() on the link with text Academic you can use the following line of code :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='nav' and #id='sticky']/div[#class='container']/ul[#class='main-nav']//li/a[#title='Academic Dashboard' and contains(#href,'../MEFAcademicDash/StudentUI/StudentHome.aspx')]"))).click();
Can you try below-mentioned xpath?
//*[text() = 'Academic']
Also, use actions driver to perform click action

Cannot find webelement using Selenium if they are dynamic

I'm having difficulties in locating a web element in seleniumn the html is as follows:
<div class="d2l-fieldgroup">
<div class=" vui-field-row">
<span class="d2l-field vui-label d2l-offscreen">Do not ask me again for this application</span>
<label class="d2l-checkbox-container">
<input name="dontAsk" value="0" type="hidden">
<input id="dontAsk" class="d2l-checkbox vui-input vui-outline" type="checkbox">
Do not ask me again for this application
</label>
</div>
</div>
I need to click on the checkbox or text but I just cant seem to be able to do this as I always get an error back saying:
Unable to locate element: {"method":"class name","selector"
I tried this:
WebElement do_not_ask_resourcebank = driver.findElement(By.className("d2l-fieldgroup"));
do_not_ask_resourcebank.click();
But get the following error:
no such element: Unable to locate element: {"method":"class name","selector":"d2l-fieldgroup"}
I also tried replacing d2l-fieldgroup with vui-field-row and d2l-checkbox-container
I also tried:
WebElement do_not_ask_resourcebank = driver.findElement(By.cssSelector("html/body/form/div/div[3]/div/label"));
and
WebElement do_not_ask_resourcebank = driver.findElement(By.xpath("id('confirmForm')/x:div/x:div[3]/x:div/x:label"));
but I just cannot seem to click on this element, very frustrating for a newbie like myself. Can someone point me to where I am going wrong?
If you are getting NoSuchElementException as your provided exception, There may be following reasons :-
May be you are locating with incorrect locator, you're locating actually <div> element with class name which looks like dynamically generated class name instead of actual <input type = 'checkbox'> element which you want. And other also looks unstable, You should try to locate desire element using By.id() as :-
WebElement do_not_ask_resourcebank = driver.findElement(By.id("dontAsk"));
May be when you are going to find element, it would not be present on the DOM, So you should implement WebDriverWait to wait until element visible as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dontAsk")));
May be this element is inside any frame or iframe. If it is, you need to switch that frame or iframe before finding the element as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//Find frame or iframe and switch
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name"));
//Now find the element
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dontAsk")));
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
Edited :
I havent tried the iframe yet because I am not sure what I am doing I havent used this tool much. The html code for the iframe is : <iframe id="d2l_1_69_954" class="d2l-iframe d2l-iframe-fullscreen d2l_1_67_746" name="d2l_1_69_954" src="/d2l/lms/remoteplugins/lti/launchLti.d2l?ou=6606&plugin‌​Id=f8acc58b-c6d5-42d‌​f-99d9-e334f825c011&‌​d2l_body_type=3" title="TALLPOD" scrolling="auto" allowfullscreen="" onload="this.m_loaded=true;" style="height: 314px;" frameborder="0"> how do you locate this iframe using your example above?
Looks like iframe id and name both are dynamically generated which could be change every time when you locate, you should try to locate this iframe with unique locator, I think it's title name is unique and fixed so you should try to locate this iframe before locating desired element as :-
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='TAFEPOC']"));

Selenium, JAVA - how to find such element?

Do you know how to find and click at an element like this:
<div class="offer ctrlOpenOfferInfo">
<a class="linkOffer" href="offer_view.html?id=1007"/>
<p class="photo">
<p class="name">New offer</p>
<p class="price">123</p>
<div class="rate ctrlViewRateOffer" data-value="0.0000">
<p class="date"/>
<div class="hide info">
<div class="bgInfo"/>
using Selenium WebDriver and using the name of the element as there are few very similiar elements on the page?
Here's what I've tired so far:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[text()='New offer']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[#id='formDevelopmentElementAdd'][text()='New offer']")));
driver.findElement(By.xpath(".//*[#id='formDevelopmentElementAdd']/div/p[2][text()='New offer']")).click();
or:
driver.findElement(By.xpath("//p[#class='name'][text()='New offer']")).click();
or:
String address = driver.findElement(By.xpath("//*[contains(text(), 'New offer') and #class='name']")).getAttribute("href");
driver.get(baseUrl + address);
or:
driver.findElement(By.xpath("//a[Text()='New offer']")).click();
I've tried these with text, class, xpath...
Assuming you want to click the p tag containing New offer:
I would think this element is not inside an iframe. If so, you must use driver.switchTo().frame("name or xpath or css for frame locator"); first before start looking for element.
Now, how can you find that element. Consider the following options:
Using xpath text based search
//p[.='New offer']
//to be more precise you can do
//p[#classs='name'][.='New offer']
You can use By.className('name') to identify that element even though that name does not look like unique to me
Using cssSelector
.name
using nth-child() function of cssSelector
.offer.ctrlOpenOfferInfo>a>p:nth-child(2)

Categories