How to extract a the data-code attribute from a hidden element - ruby

Using Watir-Webdriver, I want to be able to extract the 'data-code' value so I can enter it into the textfield on the page to unlock my locked account. Usually the code is emailed but in our test environment the code is hidden on the page, <span data-code="123456">. I want to grab the '123456' code and put it in the code textfield.
I've been unable to figure out how to do this. I'm guessing because there is no way to actually access the data code?
Is it possible to do that if this is the HTML?
<h1>
Unlock your account
</h1>
<h2>
Please enter the 6 digit code that was just sen…
<span data-code="123456">
test#email.com
</span>
</h2>

You can use the attribute_value method:
browser.span.attribute_value('data-code')
#=> 123456

You can retrieve data-* attributes similar to other standard attributes like id, name, etc. The method name is simply the data attribute with the dashes replaced by underscores:
p browser.span.data_code
#=> "123456"

Related

( Laravel ) Break lines not working ; Balise aren't displayed anymore but it won't start a new line

I'm trying to break a line but it's always displayed whatever I do so I guess I'm doing something wrong.
Here is my line from my php file
But it's displayed like this :
My site is designed to help content creators on the web, create a free account and start \nmaking money now.
Can you help me please ?
Replace \n with <br>
Use <p></p>
My site is designed to help content creators on the web, create a free account and start making money now
Use CSS white-space property for \n (example)
try to use <br> on blade, and if you use {!! !!} i think that must be inner html inside your string. cmiiw
You can save your html text as variable like this
$subtitle_welcome = "<p>My site is designed to help content creators on the web, create a free account and start </p> <p> making money now.</p>";
OR
$subtitle_welcome = "My site is designed to help content creators on the web, create a free account and start <br/> making money now.";
And you have to show in blade
{!! $subtitle_welcome !!}

Extracting links (get href values) with certain text with Xpath under a div tag with certain class

SO contributors. I am fully aware of the following question How to obtain href values from a div using xpath?, which basically deals with one part of my problem yet for some reason the solution posted there does not work in my case, so I would kindly ask for help in resolving two related issues. In the example below, I would like to get the href value of the "more" hyperlink (http://www.thestraddler.com/201715/piece2.php), which is under the div tag with content class.
<div class="content">
<h3>Against the Renting of Persons: A conversation with David Ellerman</h3>
[1]
</p>
<p>More here.</p>
</div>
In theory I should be able to extract the links under a div tag with
xidel website -e //div[#class="content"]//a/#href
but for some reason it does not work. How can I resolve this and (2nd part) how can I extract the href value of only the "here" hyperlink?

how to remove everything after specific text with xpath

I am trying to setup a Telegram Instant View for a website.
i have something like this code and want to remove everything after "remove from here" text
<p> sample text <p> test</p> remove from here <p>test text</p> </p>
how can i access every text/nodes after this specific text ("remove from here") and remove them?
Update:
i want to have this result:
<p> sample text <p> test</p> remove from here</p>
how can i access every text/nodes after this specific text
You can use following-sibling::* from XPath to access the nodes on the same level after the one you selected.
Then use #remove function from the Instant View DSL:
$selected_node: //*[self::text() and normalize-space()="remove from here"]
#remove: $selected_node/following-sibling::*
You may want to be more specific with the $selected_node. Depending on your needs, you may want to add predicates to remove only certain types of the following siblings, for example: following-sibling::*[self::node() or self::text()].

Capybara: How to fill a textarea without an 'id'?

I am in need to fill_in a textarea which does not have an id. Following the inspection:
<textarea class="stock-description-input js-short-description-textarea" placeholder="Select a product or enter a description" maxlength="64"></textarea>
Have you got an idea how to do it?
There are different location techniques and ways to get to the desired element. stock-description-input class looks like a good thing to rely on. Use send_keys() to fill the area:
text_area = first(:css, 'textarea.stock-description-input').native
text_area.send_keys('Test')

Find text in unordered list and click on link within that list item

In a ruby script that uses watir, I'm trying to find text within an list item within an unordered list, then click on the link within that list item. In the sample below, I want to find "Apple" and have it click on the link that takes you to http://samplegrocerystore.com/tehnc34.
<div class="col-login">
<img class="logo" src="/images/hello.png" />
<h1 style="margin-top:15px;">Select Account</h1>
You have multiple accounts -- please select the one you would like to log in with.
<ul>
<li><h3>The Grocery Store</h3><h4>Chapter: Apple (#01)</h4>
<br/><strong>Login to this account</strong>
</li><li>
<h3>The Grocery Store</h3><h4>Chapter: Banana (#02)</h4>
<br/><strong>Login to this account</strong>
</li>
</ul>
</div>
I guess you are saying that you don't know the link href ahead of time?
Assuming you have opened the page in question using a Watir::Browser variable called browser:
Working off the unique text in the list item (Apple),
browser.li(:text, /Apple/).link(:text, 'Login to this account').click
should click the desired link.
Or if you do know the link href ahead of time, it is easier to use that:
browser.link(:href, 'http://samplegrocerystore.com/tehnc34').click
Either one should work, though if your web page is fairly complicated using a regex to find the word Apple might be slowish.
Based on your HTML, you can use the find method on a lists collection to locate the element with the specified text and then click on the link within the list element:
li = lists.find { |el| el.text.include? "Apple"}
li.a.click
try the following in irb:
browser.h4(:text => /Apple/).links.count
browser.h4(:text => /Apple/).parent.links.count
browser.h4(:text => /Apple/).parent.parent.links.count
until you find your answer with just 1 link, then that's your immediate link following the 'Apple' text. Then just call it like below:
browser.h4(:text => /Apple/).parent.link.click

Resources