I would like to catch a text within the field and be able to click on that element. It extracts all the elements' texts into log when I use the following:
String text;
text = HomePageFields.TableOneColumn(driver).getText();
System.out.println("Table One Column contains following:\n" + text);
The TableOneColumn xpath is on different class:
public static WebElement TableOneColumn(WebDriver driver) throws IOException {
element = driver.findElement(By.xpath("//div[contains(#eventproxy,'isc_QMetricsView_0')]/div[1]/div[1]/div[1]/div[1]/div[1]/div[contains(#style,'position')]/div"));
return element;
I tried to use:
HomePageFields.TableOneColumn(driver).findElement(By.linkText("RFI Overview")).click();
But it gives an error saying won't find the element.
Here is the html link to that particular text. But other text contain in the same tag but different locations within that main tag.
Actually By.linkText() locates <a> elements by the exact text it displays while desire text is not inside any <a> tag. That's why you're in trouble.
You should try using By.xpath() with this text as below :-
WebElement el = driver.findElement(By.xpath(".//div[descendant::td[text() = 'RFI Overview']]"));
System.out.println("Table One Column contains following:\n" + el.getText());
el.click();
Or
WebElement el = driver.findElement(By.xpath(".//div[normalize-space(.) = 'RFI Overview']"));
System.out.println("Table One Column contains following:\n" + el.getText());
el.click();
Or As I'm seeing in provided screenshot desire <div> has id attribute which value looks like unique. If this attribute value is fixed for this element, you can also try using By.id() as :-
WebElement el = driver.findElement(By.id("isc_3BL"));
System.out.println("Table One Column contains following:\n" + el.getText());
el.click();
driver.findElement(By.xpath("//td[.='RFI Overview']")).click();
I'd suggest using the div with id isc_3BL as well, but I am not certain that is a static id. If it is, you could definitely use it to isolate from any other outside table containing the same exact td with text "RFI Overview"
Related
i am struggling with find sub tag name
i want to find "ankit patel" name from using below code
enter image description here
i am writing my code for as below
List<WebElement> rows= driver.findElements(By.id("ui-id-2") ); //Printing the size of the rows
List<WebElement> lirowsx = rows.findElements(By.tagName("li"));
System.out.print(lirowsx .size() +"size ");
int s=lirowsx.size();
when i am trying to access "ankit patel" using "li" tag then it returns and error message
like
Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable
(Session info: chrome=80.0.3987.132)
i have also tried with xpath but didn't get result.
can you please guide me how can i solve this ?
You can find the element by first finding it by xpath and then fetching its text.
You can do it like:
WebElement element = driver.findElement(By.xpath("//li[#class='ui-menu-item']//strong"));
// Fetching the text
String text = element.getText();
If the li element id is unique then
WebElement element = driver.findElement(By.xpath("//li[#id='ui-id-11']//strong"));
or
WebElement element = driver.findElement(By.xpath("//li[#id='ui-id-11']/div/div/strong"));
and to get the text
String text = element.getText();
Feel free to post any more errors you see...
I'm parsing html of a website with JSoup. I want to parse this part:
<td class="lastpost">
This is a text 1<br>
Website Page - 1
</td>
I want like this:
String text = "This is a text 1";
String textNo = "Website Page - 1";
String link = "post/13594";
How can I get the parts like this?
Your code would only get all the text that is in the td elements that you are selecting. If you want to store the text in separate variables, you should grab the parts separately like the following code. Extra comments added so you can understand how/why it is getting each piece.
// Get the first td element that has class="lastpost"
Element lastPost = document.select("td.lastpost").first();
// Get the first a element that is a child of the td
Element linkElement = lastPost.getElementsByTag("a").first();
// This text is the first child node of td, get that node and call toString
String text = lastPost.childNode(0).toString();
// This is the text within the a (link) element
String textNo = linkElement.text();
// This text is the href attribute value of the a (link) element
String link = linkElement.attr("href");
My html page code consists alot of anchor tags, but i need to get all the href value inside the anchor tag and anchor tag value which are present inside a header tag of a div element, i'm using selenium in java to get the page source of the html.
The part of html code of my the webpage look like this :-
qq
ww
ee
<div class="hello">
<h2>
aa
aa
</h2>
<div>
The java code i'm using to retrieve the anchor tag values look like this :-
List<WebElement> list = driver.findElements(By.xpath("//*[#href]"));
for (WebElement e : list) {
String link = e.getAttribute("href");
System.out.println(e.getTagName() + "=" + link);
}
The output of the above code is look like this:-
a=www.qq.com
a=www.ww.com
a=www.ee.com
a=www.aa.com
a=www.ss.com
But the output i need is like this:-
a=www.aa.com , aa
a=www.ss.com , ss
I need to get the all the anchortag values and href values inside the hello class
Try this -- Use getText() and modified xpath to include hrefs inside the div with class hello. Assuming the particular div is the unique one with the class name.
List<WebElement> list = driver.findElements(By.xpath("//div[#class='hello']//a[#href]"));
for (WebElement e : list) {
String link = e.getAttribute("href");
System.out.println(e.getTagName() + "=" + link + " , " + e.getText());
}
I am really unsure how I can get the information I need to place into a database, the code below just prints the whole file.
File input = new File("shipMove.txt");
Document doc = Jsoup.parse(input, null);
System.out.println(doc.toString());
My HTML is here from line 61 and I am needing to get the items under the column headings but also grab the MMSI number which is not under a column heading but in the href tag. I haven't used JSoup other than to get the HTML from the web page. I can only really see tutorials to use php and I'd rather not use it.
To get those information, the best way is to use Jsoup's selector API. Using selectors, your code will look something like this (pseudeocode!):
File input = new File("shipMove.txt");
Document doc = Jsoup.parse(input, null);
Elements matches = doc.select("<your selector here>");
for( Element element : matches )
{
// do something with found elements
}
There's a good documentation available here: Use selector-syntax to find elements. If you get stuck nevertheless, please describe your problem.
Here are some hints for that selector, you can use:
// Select the table with class 'shipinfo'
Elements tables = doc.select("table.shipinfo");
// Iterate over all tables found (since it's only one, you can use first() instead
for( Element element : tables )
{
// Select all 'td' tags of that table
Elements tdTags = element.select("td");
// Iterate over all 'td' tags found
for( Element td : tdTags )
{
// Print it's text if not empty
final String text = td.text();
if( text.isEmpty() == false )
{
System.out.println(td.text());
}
}
}
I am trying to scrape the Top Stories section in google news for all the titles. In order to only get the titles in the Top Stories section, I must narrow into this tag:
<div class="section top-stories-section" id=":2r">..</div>
This is the code I use (in Eclipse):
public static void main(String[] args) throws IOException {
// fetches & parses HTML
String url = "http://news.google.com";
Document document = Jsoup.connect(url).get();
// Extract data
Element topStories = document.getElementById(":2r").;
Elements titles = topStories.select("span.titletext");
// Output data
for (Element title : titles) {
System.out.println("Title: " + title.text());
}
}
I always seem to be getting a NullPointerException. It doesn't work either, when I try to reach the Top Stories like this:
Element topStories = document.select("#:2r").first();
Am I missing something? Shouldn't this be working? I am relatively new to this, please help and thank you!
Judging from the error message (and actually looking at the page) that div tag doesn't contain an id attribute. Instead you could select based on the CSS class
Element topStories = document.select("div.section.top-stories-section").first();