document.getElementByTag - objective-c

I already used document.getElementById, but it does not work any more. I want to get the value of 0,01€ but I can not. I want to save it in my NSString *price but how.
The HTML code is
<tr class="price">
<td>0,01</td>
<td>EUR</td>
My idea was
NSString *price = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByTagName('price.td').innerText;"];

The tagName in this case is 'td' not 'price.td', but if you run document.getElementsByTagName('id') you will get all the table cells in the page which is probably not what you want.
If the page has jQuery loaded in it you can use jQuery('.price td:first').text() to get the price.
If it doesn't have jQuery but you have control of the page you can add a class to it (<td class="my-price">12.44</td>) and get it with document.getElementsByClassName('my-price')[0].innerHTML.
If you don't have control of the page and it doesn't have jQuery you will have to find the 'price' row and then get its 1-st cell by using document.getElementsByClassName('my-price')[0].childNodes[0].innerHTML.

Also, a cleaner and more flexible approach is to use the Selectors API:
http://www.w3.org/TR/selectors-api/
document.querySelector('.price td:first').innerText

Related

Click on first element contained in a div

I have a div that contains a set of dynamic elements. I want to click on the first search result.
I want to click on the first element contains in
I tried using creating a custom xPath like so but it didn't work. Any ideas here?
//div[1][contains(text(), 'listing')]
First of all It would've helped if you had provided more information.
best will be using pseudo-child like div.firstChild or if the elements are generated dynamically you can add a class and use document.querySelectorAll(".class") which will give you an array of elements that had the class.
You can use array[0] to use click on the first element.
For anyone coming across this thread here is the solution
const listings = await page.$x('//*[contains(#id,"listing_")]')

select element with changing xpath

Example Image
say I wanted to select the element with class=kbkey button red_selected sel. Its xpath from the example in the pic would be //*[#id="virtualKeysWrapper"]/div[3], so I have the following code:
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="virtualKeysWrapper"]/div[3]'))).click()
However, the div position of this element would change everytime I refresh the site. Was wondering what should I do to successfully select the element with class=kbkey button red_selected sel successfully everytime?
Avoid using index position in XPath if at all possible for this very reason. Without knowing exactly what the rest of the DOM looks like, my best guess is that you could use the following expression:
//div[#id='virtualKeysWrapper']/div[#class='kbkey button red_selected sel']
Alternatively, you could use
//div[#id='virtualKeysWrapper']/div[#sel='true']

Vue.js get element by id/ref

I have a custom component called 'menu-entry':
<menu-entry v-for="field in fields" :id:"field.id" :ref="field.id" v-bind:key="field.id" v-bind:class="[classArray]" v-bind:field="field" v-on:clicked="menuEntryClicked">
</menu-entry>
I need to get one of them (for example field.id = 2) and remove an item from the classArray.
this.$refs[2] is working for HTML elements, but not for custom elements.
this.$el.querySelector isnt working either.
is there another way to remove an item from the classArray of a specific element?
Your question it is not clear but you are trying to set id and ref to field.id so following this logic it is not necessary to do though.
You can just send the id to the method you are executing like below:
<menu-entry
v-for="field in fields"
v-bind:key="field.id"
v-bind:class="[classArray]"
v-bind:field="field"
v-on:clicked="menuEntryClicked(field.id)" // <= send the id here
>
</menu-entry>
I am not sure if i helped but regarding your question, now you can figoure out which id of element is clicked and remove it from classArray or whatever you want
2 is not a valid id selector when you use document.querySelector('#2'); maybe you can use document.getElementById('2') instead - it can work.

How to select the element in HTML tags based on its text in Behat?

I'm running the following Behat scenario:
Then I should see "Testing body" in the "strong" element
for the following HTML snippet:
<strong>Testing body</strong>
However I am getting an error:
The text "Testing body" was not found in the text of the element matching css "strong"
What is the best way to check if element contains below tags?
<em>Testing body</em>
<ol><li>Testing body</li>
</ol>
<ul><li>Testing body​​​​​​​</li>
</ul>
I am trying to use wysiwyg.feature with syntax:
Then I should see "Testing body" in the "<Element>" element with the "<Property>" CSS property set to "<Value>" in the "Pearson Content" region
Make sure the selector used is unique.
Depending on the method used you might need id|name|label|value or css selector.
I your case the selector used is too general, you need to narrow the section by adding an extra element in front of this to tell him to search in a smaller section.
For example: #myid strong -> will search strong in the element that has the id myid
Same thing for the other elements, you could have ol>li or ul>li, but if more elements are found you will need to add an extra selector in front to narrow the section.
Always check the CSS manually in the browser and make sure is unique or the element that you need is found first.
If you want to check for an element that contains some text, you could use XPath like this:
//strong[contains(text(), 'Testing body')]
You can also use a css if you can identify this section as I said above, but I need more from the html, a large section in order to get a better selector.
The following method may help:
/**
* #Given I should see :text in the :tag element
*/
public function iShouldSeeInTheElement($text, $tag) {
$this->verifyElementByXpath("//${tag}[contains(text(), '${text}')]");
}
Instead of contains, you can also use starts-with and other.
Note: I haven't tested it, so please suggest improvements if you do.

How to select a specific table cell using HTML Agility Pack

I have to pull out particular fields from cells in an HTML table. Using Firebug I was able to get the exact XPath to the cells I need (unfortunately, the cells don't have an id tag). I thought I could use DocumentNode.SelectSingleNode and pass in that path, but it doesn't seem to be working right. What am I doing wrong? Or is there a better approach to this than how I am doing it? Unfortunately, I have no experience with XPath so this is turning out harder than I expected it to be. Here's what I have so far (I know the HTML is particuarly messy, but that's not in my control to change):
Dim page As New HtmlAgilityPack.HtmlDocument
Dim node As HtmlAgilityPack.HtmlNode
page.LoadHtml(fileContents)
node = page.DocumentNode.SelectSingleNode("/html/body/form/div[6]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td[2]")
Much appreciated.
Firebug maybe fixed broken html tags.
If you want to pick and Html node,it is recommend use class or id.
For example:
//div[#class='content']//table//tr[1]/td[2]
shorten the path,and use class or id selector.
if the table has it's own id,you can use:
//table[#id='tableid']/tr[1]/td[2]
try it,you will find XPATH is interesting.