Selenium XPath with Single, double quotes and comma - selenium

Trying to create XPath for below string (string is in .properties file and picked up by framework for further identification.):
If you would like to change your answers or finish a section that doesn’t have a checkmark, click on “Change”.
Tried below XPath:
CHANGE_ANSWER = xpath://ul/li[contains(.,concat('If you would like to change your answers or finish a section that doesn',"’", 't have a checkmark',",", ' click on ','“','Change','”','.'))]
Don't know what's exactly wrong.

Related

XPath extracted from Chrome only works for first TR-element in .NET

Could someone help me to solve my problem in the following situation:
I try to get the 3. text "Sangria Vinyl Lyrics" from this webpage:
http://www.metrolyrics.com/pharrell-williams-lyrics.html
Thanks to Google Chrome I found the following XPath to extract it:
//*[#id='popular']/div/table/tbody/tr[3]/td[2]/a
If I try to use this XPath in a Addon like XPath Helper, it works for every element that I search with tr[1...to...65]
If I copy & paste this XPath to my .NET code and installed HtmlAgilityPack, I get just "nothing".
Strangely, a modified XPath (/tr[1]) to get the 1. text ("Happy Lyrics") is the only one that I get perfectly working, returning "Happy Lyrics".
Is there something special about XPath, element indexes and HtmlAgilityPack in .NET?
My code so far:
Dim doc As HtmlDocument = New HtmlWeb().Load("http://www.metrolyrics.com/pharell-williams-lyrics.html")
Dim div As HtmlNode = doc.DocumentNode.SelectSingleNode("//*[#id='popular']/div/table/tbody/tr[3]/td[2]/a")
I tried it even with the "full path XPath" like
(/html[1]/body[1]/div[2]/div[3]/div[3]/div[2]/div[1]/div[3]/div[1]/div[1]/div[2]/div[1]/div[1]/table[1]/tbody[1]/tr[3]/td[2]/a)
but with the same result. It works in the Addon but not in .NET
Any idea, what I'm doing wrong?

Katalon WebUI.SetMaskedText() randomly wrapping text

I have a field in a popup to enter a phone number with masked text: "( _ _ _ ) _ _ _ - _ _ _ _".
(Masked text means that we are using jquery to automatically format the text being entered. You can learn more here.) The underscores above are just to represent the spaces for the digits of the phone number. :)
Results
The code randomly wraps the text when entering. I've tried using the string as '8015558245' and masked as '(801)555-8245', both wrap randomly with the results:
(015)558-2458
(155)582-4580
(555)824-5801
The only suggestion I found on my many searches of the internets was to double click the element first, which doesn't effect the results. Each time I run the test in incognito mode. Occassionally it works, the rest of the time it fails.
Code
Here is my code:
TestObject enterPhoneNumber = findTestObject('path/PhoneNumberObject')
WebUI.doubleClick(enterPhoneNumber)
WebUI.setMaskedText(enterPhoneNumber, '8015558245')
Specs
I am using Katalon v5.9.1, but it wasn't working on earlier versions either.
Chrome is the browser.
I think this might be happening because WebUI.doubleClick() is targeting the middle of the element so it starts typing somewhere behind the ().
Maybe try using WebUI.clickOffset(enterPhoneNumber,x,y) with a small value of x and y (5 px for example - calculated from the top left corner of the element) so you are sure click will land somewhere near the first (_ _ _ digit place.
Another option is using
WebUI.click(enterPhoneNumber)
WebUI.sendKeys(enterPhoneNumber, Keys.chord(Keys.HOME))
WebUI.setMaskedText(enterPhoneNumber, '8015558245')
with
import org.openqa.selenium.Keys
imported.
The answer was a combination of things:
First: I needed to set the cursor to the left of the masked text right at the opening parenthesis. To guarentee this, I used the suggested clickOffset(enterPhoneNumber, 40, 5). [Thanks #Matt Mrse for your suggestion!]
This didn't fix the text wrapping when using WebUI.setMaskedText(..), but it allowed for the next part to work reliably, and that is:
Second: Text entered into the box manually was always entered in correctly. So instead of WebUI.setMaskedText(..), I just sent the whole string as if it were typed by using WebUI.sendKeys(..).
The final code, that worked reliably (all other previous attempts failed within 1-2 attempts) after five test runs in five different tests is this:
WebUI.clickOffset(enterPhoneNumber, 40, 5)
WebUI.sendKeys(enterPhoneNumber, '8015558245')
WebUI.delay(1) //For humans to be able to see that text was entered correctly
That's it!
Hope this helps anyone else with this issue!
I think there are something you need to double check:
- are the () be added automatically when you typing the phone number?
- setMaskText() is used to enter text as '**************'
I am not sure if I understand the case clearly. Normally, I deal with this case by using sendKeys keyword, which will demostrate the typing action so the phone format can be added correctly.

IntelliJ IDEA & AppCode live template nested variable issue

I have this snippet that are used in textmate:
var ${0:name} = ${1:"${2:value}"};
When this snippet get called, first the name will be selected and I have a chance to type something and change the variable name. Press TAB again it will select "value", in this case if my value is a number (the quotes is not required) I can start to type a number and overwrite "name". If my value is a string, I can press TAB again and it will select name (no quotes), I can actually type something and they will appear in the quotes. I want to know if this is doable in JetBrains software like Intellij IDEA and AppCode. I've tried but failed. Below is my code for JetBrains software.
var $name$ = "$value$";
I don't know how to give me a chance to select "value" instead of directly selecting value. Anyone can point me to the right direction? Thanks.
I don't think you can do exactly what you want. I got fairly close using this template:
var $name$ = $QUOTE$ $value$ $QUOTE$;
Then in "Edit variables" I set the expression for QUOTE to be "\"" and moved value above QUOTE.
This allows you to type the name, as you wanted, and tab next to value, where you can enter a string or a number. The next tab will highlight the first quote, which you can delete if you had a number - this will also delete the trailing quote.
However this is not exactly what you wanted, as the value inside the quotes, should you leave them there, has spaces either side. This is because what I really wanted was
var $name$ = $QUOTE$$value$$QUOTE$;
but if you put two variables next to each other like this, IntelliJ gets very confused.
I know this isn't the answer, but maybe it has you thinking about slightly different approaches to the problem.

Trying to scrape item from website

I was attempting to create a simple program that would pull a text item from a website and add it to the textbox. I'm simply just experimenting and thought I could do it but it is not that easy for me. I know how to get the entire source code of a website(below). It has a id I know but it does not have a tag name. So Im not really sure how to make it read through the text and only keep the part next to the id . Or would it be better to use a Webbrowser tool and then try and get the text item like that. I'm just trying to do whatever is faster. I think my 1st option is better because it would be better for the computer's ram. Using the code below I don't know what to add next?
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("Website")
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
Dim source As String = sr.ReadToEnd()
Lets say the id is "name" for example. Viewing the source of the page this is what the part looks like(below). How can I parse through the source which is a string and find this section, get the name Brandon, and add it to the textbox.
<span id="name">Brandon</span>
There are a few ways to go about this. I'm not going to write any source code though since I haven't used Visual Basic in a very long time. But if you Google for how to do any of the following you should find many tutorials and documents on it.
Regular Expressions
Using a Regular Expression on the full source code can help you find the element by searching for the ID attribute which should be unique. Regular Expressions can sometimes be very slow, which is why if you have to perform a lot of searches on large sections of text, it should be avoided.
/<([a-z0-9]+)\sid="name"(.*?)>(.*?)<\// -> Not Tested, but might help you
String Position
Using a function that will find the position of a substring in a string would be useful. In C it's strstr and in PHP it's strpos. These type of functions will give you starting position of a string, in which your case would be searching for id="name". Once you find that, you will find the position of the end of the tag and then find the closing tag for that element. You then will perform a substring function that will get you the text starting at position X for the length of that you specify, which would be the closing tag position - end of opening tag position.
HTML / XML Library
There are probably a ton of HTML / XML libraries that will parse the document into some sort of object or an array. You then can loop through these elements until you find the one you are looking for. Some of these libraries may even have search functions of element ID's similar to how JavaScript will sort for a specific element.
These libraries may be hard to get started with, but they will offer you a lot of options in the future if you need to continue finding more HTML elements.

How do hard code HTML in VB for the Body of an email?

Sorry if that title wasn't very easy to get the gist of. This is my first post.
Basically within my program is the option to send an email. Its an E-Ticket. I have set 'IsBodyHtml' to true. It sends it fine. No problems at all.
Within the HTML code however I want to insert some fields that are relevant to each customer.
When I put set ETicket.Body = to the HTML Code I get a number of errors because words such as 'Width' and 'Height' etc are being picked up as VB words.
As a short term fix so I could test that the HTML body actually works I put the code into a rich text box and then set ETicket.Body = RichTextBox1.Text . It works, but doesn't have the data in it that I want.
The data relevant to each customer is held in an array. Any idea how I can get the HTML code to be accepted by VB? Or how I can get my data from the array into the relevant position in the rich text box?
Thankyou!
Joe
This will likely be due to the double quotes in the HMTL markup. Try doing a find and replace on the HTML, and replace double quotes (") with single ones (').