VBA Selenium Scraping text from label - vba

How do I extract the following "label" using the chrome driver in Selenium for VBA? The information that I want is "Character 3"
HTML:
<label for="frmentermemorableinformation1:strEnterMemorableInformation_memInfo1">Character 3 </label>

To print the text Character 3 you can use either of the following locator strategies:
Using css_selector:
Debug.Print .FindElementByCss("label[for^='frmentermemorableinformation1'][for$='strEnterMemorableInformation_memInfo1']").Text
Using xpath:
Debug.Print .FindElementByXPath("//label[starts-with(#for, 'frmentermemorableinformation1') and contains(#for, 'strEnterMemorableInformation_memInfo1')]").Text

Related

How to locate the element as per the given HTML

<input value="Order (ESHOP)" class="btn" name="order_eshop" title="Order (ESHOP)" type="button" onclick="Sfdc.logServer('CUSTOM_URL_BUTTON', {id: '00b41000002iuj6', name: 'Order_ESHOP'}, Sfdc.Logging.LogLevel.INFO);
openIntegration('/servlet/servlet.Integration?scontrolCaching=1&lid=00b41000002iuj6&eid=a0863000007SV1q&ic=1&isdtp=vw&linkToken=VmpFPSxNakF4T1MweE1TMHhNRlF5TVRvd09EbzBNaTQyT1RWYSx0eno3X1lSS1lOY1hORmtKb2ZvM3BxLFlXWmtNR0po', 'height=600,location=no,resizable=yes,toolbar=no,status=no,menubar=no,scrollbars=1', 1)">
I' am not able to find the xpath for this. I have same element on a single path Order (ESHOP). When I'am trying to use indexing it is not finding the correct xpath.
The desired element is a JavaScript enabled element so to locate/interact with the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.btn[name='order_eshop'][value='Order (ESHOP)'][title='Order (ESHOP)']")));
Using XPATH:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='btn' and #name='order_eshop'][#value='Order (ESHOP)' and #title='Order (ESHOP)']")));

How to construct an XPath which remove text from <br> tag?

I am trying to fetch text before the <br> tag using XPath & Java. I tried multiple solutions, but had no luck yet.
//div[#class="help-block"]/p[count(preceding-sibling::br) < 1]
This is my HTML code:
<div class="help-block">
<p>
This is sample text
<br>
Text after a breakpoint
</p>
</div>
Expected outcome: This is sample text.
Actual outcome: This is sample text Text after a breakpoint
To get the This is sample text Induce WebDiverWait And visibilityOfElementLocated() And get the element.
Induce JavascriptExecutor And get the textContent of firstChild
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='help-block']/p")));
JavascriptExecutor js = (JavascriptExecutor) driver;
System.out.println(js.executeScript("return arguments[0].firstChild.textContent;",element));
I think you can just run this:
driver.find_element_by_xpath("//p[br]").text
This will get the <p> element, but the query also checks to make sure that <p> contains a <br> element inside it. So you are just getting the <p>, and .text should get the text from the <p>.
Your expression was close. But you didn't take into account that p is only one element and you want subnodes of p. So change your expression to the following (also escaping the < char)
//div[#class="help-block"]/p/node()[count(preceding-sibling::br) < 1]
It's output is:
This is sample text

Clicking a radio

I checked the other posts like this but they weren't able to help me. I'm just starting out with Selenium and I'm having trouble clicking a radio button.
This is what is in the inspector.
input id="createCreds" name="addUser" aria-required="true" ng-model="formData.newUserType" ng-required="addNewUser.$submitted && !formData.newUserType" "="" class="ng-valid ng-valid-required ng-dirty ng-valid-parse ng-touched" aria-checked="true" aria-invalid="false" value="createCreds" type="radio"
Here is what I've tried
Trial 1 :
WebElement userRadioBtn = driver.findElement(By.id("createCreds"));
userRadioBtn.click();
Trial 2 :
driver.findElement(By.xpath("//*[#id="createCreds"]']")).click();
Trial 3 :
driver.findElement(By.id("createCreds")).click();
Trial 4 :
input[#value='createCreds']/following-sibling::label
I was trying to copy this guy around 6 minutes.
https://www.youtube.com/watch?v=BKbzW4S2qQ0
I was hoping to find something like Click RadioButton css=#createCreds or xpath=//*[#id="createCreds"]
As per the HTML you have shared to click on the Radio Button as the element is an Angular element you have to induce WebDriverWait and you can use either of the following options as follows :
cssSelector :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.ng-valid.ng-valid-required.ng-dirty.ng-valid-parse.ng-touched#createCreds"))).click();
xpath :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='ng-valid ng-valid-required ng-dirty ng-valid-parse ng-touched' and #id='createCreds']"))).click();

Toggle the element - Selenium

I am struggling with Selenium.
Basically I want to click on the following element to toggle the element:
<div class="btn-group pull-right">
<a class=" mr-2" aria-expanded="false" aria-haspopup="true" data-toggle="dropdown-ddd" href="javascript:void(0)" onclick="$(this).closest('.btn-group').toggleClass('open');"
<svg class="">
<use xlink:href="resources/icons/settings.svg#Layer_1" xmlns:xlink="http://www.w3.org/1999/xlink">
</svg>
</a>
It changes to:
<div class="btn-group pull-right open”>
<a class=" mr-2" aria-expanded="false" aria-haspopup="true" data-toggle="dropdown-ddd" href="javascript:void(0)" onclick="$(this).closest('.btn-group').toggleClass('open');">
<a class=" mr-2" aria-expanded="false" aria-haspopup="true" data-toggle="dropdown-ddd" href="javascript:void(0)" onclick="$(this).closest('.btn-group').toggleClass('open');">
<svg class="">
<use xlink:href="resources/icons/settings.svg#Layer_1" xmlns:xlink="http://www.w3.org/1999/xlink">
</svg>
</a>
The method I wrote for clicking this element:
WebElement dropDown = driver.findElement(By.xpath("//div[#class='btn-group.pull-right’]”));
dropDown.click();
However, I am getting an Exception:
no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='btn-group.pull-right']"}
Any recommendations on why I am geting this exception?
You can use 'or' logical operator in ur xapth which locate both.
Try this Xpath :-By.xpath("//div[#class='btn-group.pull-right’|//div[#class='btn-group pull-right open’ "]
your html shows element has class "class="btn-group pull-right" "
and your xpath "By.xpath(" //div[#class='btn-group.pull-right’]”" if i closely look at it your are adding dot in the xpath to remove space which is not required when using xpath, spaces will be removed by dot in css. So try using this xpath
//div[#class='btn-group pull-right’]

Using Selenium, xpath gets tags but text is empty

I'm using Selenium WebDriver to fetch a bunch of tags via XPATH. The xpath successfully finds the tags, but the "Text" attribute for the returned IWebElements is empty.
Here's the HTML:
<ul>
<li id="foo1">someValue</li>
<li id="foo2">someOtherValue</li>
</ul>
And the xpath:
//ul/li[startswith(#id, 'foo')]
Any ideas? The Xpath definitely grabs the right elements, but the Text element is empty.
Try this code to get the text of 2nd element - <li id="foo2">someOtherValue</li>
:
WebElement fooEle = driver.findElement(By.xpath("//descendant::ul/li[starts-with(#id,'foo')][2]"));
String fooEleText = fooEle.getText();
System.out.println("foo Element Text -" + fooEleText); //should print expected text "someOtherValue"
Note- if you want to get the 1st element text, then change the index value of the xpath
That is, //descendant::ul/li[starts-with(#id,'foo')][1]