webdriver - how to print all the main links in the page - webdriver

i want to print all the main module links ...but i was getting all the links printed if take the frame and then extarct all the links using tag "a".
please correct me..
List All_Module_Links=driver.findElements(By.xpath("//*[#id='top-menu']"));
WebElement modules_box = All_Module_Links.get(0);
List links =modules_box.findElements(By.tagName("a"));
System.out.println("Total links are : "+links.size());
for(int i=0;i {
System.out.println(links.get(i).getText());
}![enter image description here][1]

WebElement topmenu = driver.findElements(By.xpath("//*[#id='top-menu']"));
List<WebElement> links =topmenu.findElements(By.tagName("a"));
System.out.println("Total links are : "+links.size());
for(int i=0;i<links.size();i++)
{
System.out.println(links.get(i).getText());
}

Related

Dynamic attributes using bootstrap/css design. We are not able to perform action using selenium

As per the above code, I have tried to locate and perform action on elements in Selenium.
Actions action = new Actions(driver);
Putting Control to Elements
WebElement we = driver.findElement(By.xpath("//li[#class='static dynamic-children']/a[#href='/activa/gentex/ee/#']/span[#class='additional-background']"));
action.moveToElement(we).build().perform();
WebDriverWait wait = new WebDriverWait(driver, 50);
// wait for the edit employee information to appear
wait.until(ExpectedConditions.presenceOfElementLocated(By
.xpath("//a[contains(#class,'selected')]/span[#class='additional-background']/span[#class='menu-item-text']")));
// action.moveToElement(driver.findElement(By.xpath("//div[#id='zz1_TopNavigationMenuV4']/div/ul/li/ul/li/ul/li[2]/a/span/span"))).build().perform();
// action.click(driver.findElement(By.xpath("//div[#id='zz1_TopNavigationMenuV4']/div/ul/li/ul/li/ul/li[2]/a/span/span"))).perform();
Moving control to Child menu item to click on it.
driver.findElement(By.xpath("//a[contains(#class,'selected')]/span[#class='additional-background']/span[#class='menu-item-text']")).click();
It seems you have incorrect xpath , Please check correct code below :
Actions action = new Actions(driver);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement Mainmenu= driver.findElement(By.xpath("//*[#id='zz1_TopNavigationMenuV4']/div/ul/li/ul/li[1]"));
action.moveToElement(Mainmenu).build().perform();
WebElement submenu1 = driver.findElement(By.xpath("//*[#id='zz1_TopNavigationMenuV4']/div/ul/li/ul/li[1]/ul/li[1]/a/span/span"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='zz1_TopNavigationMenuV4']/div/ul/li/ul/li[1]/ul/li[1]/a/span/span")));
submenu1.click();
I am hovering mouse on main menu : My Benefit Resources and then clicking on child menu : Enrollment. Above code should work fine for you.
For reading the tags try this :
<span>xyz</span>
You can try this:
re-frame the xpath as
//span[contains(.,'xyz')]
It should help.

Find element within table cell in webdriver using java

I want to check if there is a clickable image present in the table cell.
If(clickable image present)
{
//
}
else
{
//
}
How do I do it in webdriver using Java?
Does your image tag have a name or id associated with it ?? Please post some code to help us help you.
in that case use
WebElement image = driver.findElement(By.name("imagename"));
image.click();
or
WebElement image = driver.findElement(By.id("imageid"));
image.click();
This identification can be done on name,id,class,xpath etc.
If not you may have to use xpath or css selectors
WebElement image = driver.findElement(By.xpath("xpath"));
image.click();
You can check if the image is clickable by using
WebDriverWait wait = new WebDriverWait(yourWebDriver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//xpath_to_element"));
and then perform element.click().

Applying Css Style to Asp:Menu MenuItem

I'm essentially creating a vertical breadcrumb to create a website navigation for a mobile (iphone) website. Similar to maybe how http://news.bbc.co.uk/sport1/hi/football/default.stm works as you click into "Premier League"
Using the Asp:Menu control and a SiteMapDataSource I am binding only the current levels links within the sitemap and then finding their parent to manually insert at the top of the list. An example resulting nav would be:
About,
Who Are We,
What We Do,
Locations
var mi = new MenuItem();
mi.NavigateUrl = node.Url;
mi.Text = node.Title;
mi.ToolTip = node.Description;
MobileMenu.Items.AddAt(0, mi);
This is all fine and works perfectly. However, this dynamically inserted top MenuItem needs to be styled in a different background colour. In the example above "About" would have a darker bg colour than the 3 items below it.
But there isn't any kind of obvious property on the MenuItem to do this.
How could I dynamically set a style on the MenuItem that I am inserting into position 0?
In answer to this I used the jQuery li:nth-child() method to set a class to the li after which I then use Page.ClientScript.RegisterStartupScript().
private const string HighlightScript =
"<script language=\"javascript\">\n" +
"$(\"ul.top li:nth-child(4)\").addClass(\"menu-Fourth\");" +
"</script>";
public void AddHighlightScript(string script, string name)
{
Page.ClientScript.RegisterStartupScript(GetType(), name, script);
}
If someone else has a solution please share.

Detect and Activate Hyperlink in ASP.Net

I have a ASP.Net page for user to post their comments. Now I want that once user posts comments if any hypelink is found in the posted text then how can we make the hyperlink clickable.
Right now we are displaying the text and hyperlikn is not clickable
Use regular expressions to find hyperlink patterns. Then re-save the content with the html a tags.
EDIT: Here is an example to get you started, Run this as a console app to see whats going on:
class Program
{
static void Main(string[] args)
{
string s = "http://www.google.com is the best site, followed then by http://www.yahoo.com";
string pattern = #"http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\#\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?";
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern);
System.Text.RegularExpressions.MatchCollection matches = regex.Matches(s);
for (int i = 0; i < matches.Count; i++)
{
Console.WriteLine(string.Format("{1}", matches[i].Value, matches[i].Value)); }
}
}
The regular expression pattern was taken from: http://weblogs.asp.net/farazshahkhan/archive/2008/08/09/regex-to-find-url-within-text-and-make-them-as-link.aspx
response.write("The Link")

How to remove or disable hyperlink events from pdf file?

I can display a PDF file from byte[] in asp.net.
The problem is that it contains hyperlinks and I want to disable or remove these hyperlink events.
Docotic.Pdf, the library I am involved with, can be used to find hyperlinks in PDFs and remove them.
Here is the sample code that does exactly this:
public static void RemoveHyperlinks(string inputFile, string outputFile)
{
using (PdfDocument doc = new PdfDocument(inputFile))
{
foreach (PdfPage page in doc.Pages)
{
for (int i = 0; i < page.Widgets.Count; i++)
{
PdfWidget widget = page.Widgets[i];
PdfActionArea actionArea = widget as PdfActionArea;
if (actionArea != null)
{
PdfUriAction linkAction = actionArea.Action as PdfUriAction;
if (linkAction != null)
{
page.Widgets.RemoveAt(i);
i--;
}
}
}
}
doc.Save(outputFile);
System.Diagnostics.Process.Start(outputFile);
}
}
Please note that some viewers can detect hyperlinks from text and still present them as clickable areas even though there is not links defined in PDF itself. For example, Adobe Reader with certain settings can do just that.
P.S. I know this question is old, but maybe my answer will benefit new visitors.
You can libraries like this one to open and modify the PDF file and convert every hyperlink object to simple text.

Resources