java Selenium Chrome - keyword searcher - java

I'm almost done creating a Supreme Bot. Now I need a keyword-searcher. They should search for a keyword on the page and then click on it.
For example:
Illegal Business Hooded Sweatshirt Red
... the bot now searches for the keyword but also for the color. I uploaded a screenshot (from the Supreme page) and need your help.
Screenshot from the source code (Supreme):
My code I tried:
driver.findElement(By.xpath("//h1[text()='Illegal Business Hooded Sweatshirt']/p[text()='Red']")).click();

Since there're encoded symbols in between of text, I believe you can't find those items directly with xpath.
I suggest you to find all <acticle> tags, then for every article tag you search for <h1> inside, retrieve it's text (with filtering out those weird symbols), and compare the text you want with the text article tag actually has.

p tage is not inside h1 tag and "red" is inside anchor tag a
So you can use this xpath:- //h1/a/[text()='Illegal Business Hooded Sweatshirt']/ancestor::div/p/a[text()='Red']

Related

Why gettext() in selenium is returning a-singlespace-b instead of a-multiplespaces-b?

I am testing TodoMVC page's todo list and I came across a problem where in the DOM label tag of the element is having a text with many spaces between two letters, but on the UI it is showing letter single space then another letter, and when gettext() is performed on the element we are getting "a b" which is visible on the UI instead of the text present in label tag of that element.
This is how Selenium WebDriver works. Since it designed for UI testing, all interactions with Browser data are made from a user perspective.
Here is an extraction from Get Element Text W3C specification:
NOTE
The Get Element Text command intends to return an element’s text “as rendered”. An element’s rendered text is also used for locating a elements by their link text and partial link text.
One of the major inputs to this specification was the open source Selenium project. This was in wide-spread use before this specification written, and so had set user expectations of how the Get Element Text command should work. As such, the approach presented here is known to be flawed, but provides the best compatibility with existing users.

Unable to Identify All Text Elements on Page with XPath

I'm trying to identify all text elements on web page. Based on several threads I've read on stackoverflow I've come up with the following XPath:
//*[normalize-space(.)=.][not(self::script or self::style or self::meta)]//.
//*[normalize-space(.)=.][not(self::script or self::style or self::meta)]//text()
However, I noticed that while this identifies several hundred text elements on Amazon product page, it leaves out some important ones.
For example:
On this page I am not able to identify the 'features' section and order by time section:
"Echo Show brings you everything you love about Alexa.."
'Want it tomorrow.."
On this page I cannot identify the description section (immediately to the right of product image)
Both of these pages have text that are prominently displayed on the page so I do not see why it is not identifying them accordingly.
I'm aware that some of these text are under ul/li tags but I don't think this is the issue.
Finally, would it be possible to combine the said XPath with another attribute value so it only identifies text elements with given attribute value (i.e, //*[normalize-space(.)=.][not(self::script or self::style or self::meta)]//. + .//*[#id='XYZ'])
Thanks
Try using:
//*[text()[not(normalize-space()='')]]
That will select any element that has a text() node as a direct child that isn’t just whitespace.

Accessing an element with unknown id but known path

I am working on a testing program that operates with very little information. In this particular case, my program doesn't know the ID of elements in the page before it runs, because the Javascript on the page dynamically assigns those at run time. The only constants I have is the structure and the text I'm looking for. I'm including a screenshot of one example of the DOM being generated. In this case I know that I want to access the button with text apply that is displayed next to the label with the text "To Location:" Is there a way to use xpath manipulate their relationship and ensure that I'm accessing the right element. I can't just access the apply button because there are 6 apply buttons on the page with dynamically generated IDs. The label's next to them are different so I'm trying to use that and manipulate the path's from there. Help?
This is possible. If you provide the entire html code I could provide a better xpath. But for what you pasted, here's a simple one that might work:
//td[div//label[text()='To Location:']]/following-sibling::td[1]//button[text()='Apply']
There's a slightly longer winded way but thats generating a list of elements by class and clicking the one with the right text
`var elements = driver.FindElements(By.Class("text-pb"));
foreach(var element in elements)
{
if(element.Text.Equals("Searched Text"))
{
element.click();
}
}`
that might work thats if you want to click the button.
i use these sort of things on the pages works site generates so it should do what your after.

Print html portion into pdf using Java

community!
My project is simple: I have a link to a website that has multiple information on different chemical substances and I want to extract some data and put in into pdf. Thing is that I want to keep the formatting of the original HTML (using it's css, of course).
Example of substance: http://www.molbase.com/en/msds_1659-31-0-moldata-2.html#tabs
I used jsoup to read the HTML of the table on the bottom of the page, the MSDS one, containing multiple sections with different information about the substance, but I really don't know how to save the exact HTML format into my pdf file. I have tried with iText too, but it gives me "missing ending tag" error, and if it worked, it would print the full page, not only that msds table.
Here is what I have tried to do, but ain't effective:
Document docu = Jsoup.connect(urlbun).get();
Element tableHeader = docu.select("div[class=\"msds\"]")
.first();
String[] finSyn = tableHeader.text().split(" ");
String moreText =" ";
I tried to split the text that the webpage has under that div ("class = "msds"") but I cannot find a way to split it the good way.
Please, could you please give me a hint on what to do? Even if the formating is not the same, I would like to be able to display the information in the same way, with indentation and such.
Thank you!
You can put the content that you want to convert to PDF inside a CSS ID (such as a DIV) and then use the PDFmyURL API to convert only that section to PDF.
Please refer to this on our website about how to select pieces from a page to convert to PDF
Disclosure: I work for the company that owns this site

How to display part of an HTML document in Java

I have an application where I need to show one specific section of a HTML document within a swing JPanel. The section to be shown depends on what the user is doing at any given time.
I know that JEditorPane can display simple HTML, and in fact in terms of HTML support this is more than enough for my needs. However I don't think I can use this to display only part of the original HTML file.
I thought of putting each section within a div, then hiding all divs with CSS (display: none), and showing only the target section by setting display: block on the section I wanted to show. Unfortunately JEditorPane has limited CSS support and this does not seem to include the "display" attribute.
Before I go and implement something more elaborate, is there any simple way to achieve this goal?
Thanks.
You may try Cobra :
http://lobobrowser.org/cobra.jsp
Override the ViewFactory and replace DIV views. If they should be hidden let them return 0 from getXXXSpan methods.
See for example the section folding related code http://java-sl.com/collapse_area.html
I didn't find a way to do what I wanted relying on the CSS support from the JEditorPane. What I ended up doing is manually parsing the HTML document and splitting it in "fragments" (top-level DIVs representing sections), then displaying each section as required via JEditorPane.setText.

Categories

Resources