I have a problem is I can not store the value of href on the page
<a target="_blank" href="http://xxx.xx/RLS?mid=-1050286007&guid=53v90152oyA8bDg&lid=26527875" clinkid="26527875"></a>
How can I take the value of href using findElement ?
You should try using getAttribute after finding element as below :-
String href = driver.findElement(By.cssSelector("a[clinkid = '26527875']")).getAttribute("href");
Edited 1:- if clinkid dynamically generated try using by visible link text as below :-
String href = driver.findElement(By.linkText("your link text")).getAttribute("href");
or
String href = driver.findElement(By.partialLinkText("your link text")).getAttribute("href");
or
String href = driver.findElement(By.cssSelector("a[target = '_blank']")).getAttribute("href");
Edited 2 :- if you want query string from url, you should implement java.net.URL to parse it into url and then use getQuery() to get query string as below :-
URL url = new URL(href);
String queryStr = url.getQuery();
Hope it helps..:)
Related
I have below HTML code:
<div class="card-platform">
<span class="sr-only">Hosted at the </span>
<!-- react-text: 365 -->
ANDROID
<!-- /react-text -->
<span class="sr-only"> app store</span>
</div>
</div>
I want to retreive the word "ANDROID". There are many elements which have "ANDROID" value. The other value for the HTML element is "IOS". Few have ANDRIOD and few others have IOS. I want to print what either ANDRIOD/IOS for each element whatever is defined in the HTML.
How to get this value using selenium?
Required text located not inside "react-text" tag as that element is just comment, but text is located inside a div. You can use below code to get required value:
WebElement MyText = driver.findElement(By.xpath("//div[#class='card-platform']"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
String platformName = (String) jse.executeScript("return arguments[0].childNodes[4].nodeValue", MyText);
This is a simple exercise in string manipulation:
String fullText = driver.findElement(By.className("card-platform")).getText();
assert fullText.equals("Hosted at the \nANDROID\n app store");
String prefix = driver.findElements(By.className("sr-only")).get(0).getText();
String suffix = driver.findElements(By.className("sr-only")).get(1).getText();
assert prefix.equals("Hosted at the ");
assert suffix.equals(" app store");
String yourText = fullText.replace(prefix, "").replace(suffix, "");
assert yourText.equals("ANDROID");
Of course you will have to adjust the locators for all your different cases. This is based on the tiny sample of code snippet you provided.
In a similar situation, I got a number using the code below.
from requests import get
from bs4 import BeautifulSoup
pagina = "https://example"
response = get(pagina)
soup = BeautifulSoup(response.text, "html.parser")
cont = soup.find(class_="Trsdu(0.3s)")
print(cont.text)
I want to get URL's of hyperlinks present on current page using Selenium Web driver. Can anyone help.
To get the URL of all the links on a page, you can store all the elements with tagname 'a' in a WebElement list and then you can fetch the href attribute to get the link of each WebElement.
you can refer to the following code :
List<WebElement> links = driver.findElements(By.tagName("a")); //This will store all the link WebElements into a list
for(WebElement ele: links) // This way you can take the Url of each link
{
String url = ele.getAttribute("href"); //To get the link you can use getAttribute() method with "href" as an argument
System.out.println(url);
}
Just get them from the href attribute using getAttribute() (assuming you are in java):
WebElement link = driver.findElement(By.tagName("a"))
String url = link.getAttribute("href")
I am new to selenium. I am trying to retrieve the value of a textbox. Below is my code.
WebElement e = driver.findElement(By.id("id"));
e.sendKeys("text");
String str = e.getAttribute("value");
System.out.println(str);
The above code is working fine in all sites but is not working for a particular site. I can't share the site details.
Any explanation regarding why the code is not working for a site or is there another way to get the text from a textbox?
getAttribute('value') will provide you with null cause html snippet doesnot contains value attribute or the DOM object of that element has no value attribute. Try getting the text with :
String str = e.getText(); //If it helps
OR
use JavascriptExecutor as
JavascriptExecutor js = (JavascriptExecutor) driver;
String str = js.executeScript(
"return document.getElementById('company').value")
.toString();
Hi i want to get value of html element using web driver how can i get it?I am explaining the scenario as below. I have a span element as below with value between the starting and closing tag .How can i get it?
<span id="foo">
some value
</span>
You have to use the webElement.getText() for that.
I worte a small unit test for you:
public class TestGetText
{
#Test
public void shouldReadSomevalue()
{
final WebDriver webDriver = new HtmlUnitDriver();
webDriver.get("http://s2server.de/stackoverflow/11719445.html");
final WebElement webElement = webDriver.findElement(By.id("foo"));
final String text = webElement.getText();
assertEquals("some value", text);
}
}
Try below solution -
String test = driver.findElement(By.id("lbHome")).getText();
System.out.println(test);
Try locating the element using XPath instead of ID, then using either
driver.findElement(By.xpath(“xpath for your lbl“)).getText()
or
String st = driver.findElement(By.xpath(“xpath to your lbl“)).getAttribute(“value”);
Source : SeleniumWiki
I'm using selenium RC and want to get all the attributes and all. Something like:
link = sel.get_full_link('//a[#id="specific-link"]')
and the result would of:
print link
would be:
<a id="specific-link" name="links-name" href="url"> text </a>
Is this possible?
thanks
Here's a fancier solution:
sel.get_eval("window.document.getElementByID('ID').innerHTML")
(don't be picky with me on the javascript..)
I think the best way to do this would be to use the getHtmlSource command to get the entire HTML source, and then use either a regular expression or HTML parser to extract the element of interest.
The following Java example will output all links to System.out:
selenium.open("http://www.example.com/");
String htmlSource = selenium.getHtmlSource();
Pattern linkElementPattern = Pattern.compile("<a\\b[^>]*href=\"[^>]*>(.*?)</a>");
Matcher linkElementMatcher = linkElementPattern.matcher(htmlSource);
while (linkElementMatcher.find()) {
System.out.println(linkElementMatcher.group());
}
getAttribute
String href = selenium.getAttribute("xpath=//a[#id="specific-link"]/#href")
I've been trying to do just this, and came up with the following:-
var selenium = Selenium;
string linkText = selenium.GetText("//a[#href='/admin/design-management']");
Assert.AreEqual("Design Management", linkText);
use below code to get all the links on the page:
$str3= "window.document.getElementsByTagName('a')";
$k = $this->selenium->getEval($str3);
$url = explode(",",$k);
$array_size = count($url);
$name=array();
$l=0;
for($i=0;$i<$array_size;$i++)
{
if(!strstr($url[$i], 'javascript'))
{
$name[$l]=$url[$i];
echo "\n".$name[$l];
$l++;
}
}
If the link isn't dynamic, then try this rather cheesy, hacky solution (This is in Python):
selenium.click("//a[text()='Link Text']")<br>
selenium.wait_for_page_to_load(30000)<br>
myurl = selenium.get_location()
Cheesy but it works.
Note: this will not work if the link redirects.