Puppeteer - xpath - How to input text in div element using xpath? - node.js

I have been struggling with this for a while but not avail.
I need to input some text in div element with xpath.
I am familiar with inputting text using id and names of elements. However, for inputting text into dynamic tables, unable to find a method using xpath. any help will be appreciated.
In the below code example, I am trying to input the text '0000'
const [ncm] = await cmtab.$x('//*[#id="sheet1"]/tbody/tr[2]/td/div/div['+ d + ']/table/tbody/tr['+ r3 + ']/td[9]');
await cmtab.evaluate(ncm, (element, value) => element.value = value, "0000");
Should be able to input the text in the table using xpath. Please help as this would mean a lot for my project.

Related

Trouble With String Concatenation With Quotations While Using Selenium

I'm using Selenium to cycle through a directory of images and save one image at a time by searching a persons name and then using this line of code to get the image.
l = driver.find_element_by_xpath('//*[#alt="John Smith"]')
file.write(l.screenshot_as_png)
I want to make it so I can change the name in the XPath so it can cycle through a list of names but every time I try to concatenate the XPath with a variable, it says the XPath doesn't exist.
This is the line of code I am using.
l = driver.find_element_by_xpath('//*[#alt="' + name + '"]')
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#alt="John Smith"]"}
Any suggestions on how to make this work?

What is the difference between 'page_source' and 'find_element_by_tag_name("body").text'?

Trying to find whether a text is present on UI login page (web-page).
I could verify it by 'driver.page_source()' and driver.find_element_by_tag_name("body").text
driver.page_source()
text = "abcd"
page_source = driver.execute_script("return document.body.innerHTML;")
if text in page_source:
return True
else:
return False
driver.find_element_by_tag_name("body").text
text = "abcd"
value = text in self.browser.find_element_by_tag_name("body").text
if value:
return True
else:
return False
What's the difference between method1 and method2 ?
Which one is preferred to do the required task ?
Which is faster ?
Or anySelenium-UI methods to be used ?
Any help would be appreciated. Looking for valuable inputs.
Any idea on this ? Any help here ?
Page source will give all the text including HTML tags, styles etc. as you have written yourself in execute script to return the innerHTML. So, all HTML code will be returned which obviously will contain the text too. You can also get the whole html with selenium too instead of using JavaScript executor by browser.page_source.
On the other hand, browser.find_element_by_tag_name("body").text will return all the text you see on the page without html tags.
To me, the 2nd method should be preferred and faster because you will have string of smaller length(without un-necessary html tags) and the actual text you are interested in.

Can't acess dynamic element on webpage

I can't acess a textbox on a webpage box , it's a dynamic element. I've tried to filter it by many attributes on the xpath but it seems that the number that changes on the id and name is the only unique part of the element's xpath. All the filters I try show at least 3 element. I've been trying for 2 days, really need some help here.
from selenium import webdriver
def click_btn(submit_xpath): #clicks on button
submit_box = driver.find_element_by_xpath(submit_xpath)
submit_box.click()
driver.implicitly_wait(7)
return
#sends text to text box
def send_text_to_box(box_xpath, text):
box = driver.find_element_by_xpath(box_xpath)
box.send_keys(text)
driver.implicitly_wait(3)
return
descr = 'Can't send this text'
send_text_to_box('//*[#id="textfield-1285-inputEl"]', descr)' #the number
#here is the changeable part on the xpath
:
edit: it worked now with the following xpath //input[contains(#id, 'textfield') and contains(#aria-readonly, 'false') and contains (#class, 'x-form-invalid-field-default')] . Hopefully I found something specific on this element:
You can use partial string to find the element instead of an exact match. That is, in place of
send_text_to_box('//*[#id="textfield-1285-inputEl"]', descr)' please try send_text_to_box('//*[contains(#id,"inputEl")]', descr)'
In case if there are multiple elements that have string 'inputE1' in id, you should look for something else that remains constant(some other property may be). Else, try finding some other element and then traverse to the required input.

How to read the value of an span element with Puppeteer

I am trying to do some web scraping reading some lines inside a html page. I need to look for a text which is repeated through the page inside some <span> elements. In the following example I would like to end with an array of strings with ['Text number 1','Text number 2','Text number 3']
<html>
...
<span>Text number 1</span>
...
<span>Text number 2</span>
...
<span>Text number 3</span>
...
</html>
I have the following code
sElements = ' ... span'; // I declare the selector.
cs = await page.$$(sElements); // I get an array of ElementHandle
The selector is working as in Google Chrome developer tools it captures exactly the 3 elements I am looking for. Also the cs variable is filled with an array of three elements. But then I am trying
for(c in cs)
console.log(c.innerText);
But undefined is logged. I have tried with .text .value .innerText .innerHTML .textContent ... I do not know what I am missing as I think this is really simple
I have also tried this with the same undefined result.
cs = await page.$$eval(sElements, e => e.innerHTML);
Here is an example that would get the innerText of the last span element.
let spanElement;
spanElement = await this.page.$$('span');
spanElement = spanElement.pop();
spanElement = await spanElement.getProperty('innerText');
spanElement = await spanElement.jsonValue();
If you still are unable to get any text then ensure the selector is correct and that the span elements have an innerText defined (not outerText). You can run $(selector) in Chrome console to check.

Watir and <script>

I am very new to programming so trying to solve the following issue with Watir:
I have a webpage that is full of fields, I'm trying to scrape values from inside ==$. The values inside start from var pageData if that helps.
X path is //*[#id="innerpage"]/script[48]
How can I achieve this?
Thanks
I don't know what ==$ means or what var pageData means, but to get the element at the provided XPath you use:
element = browser.element(id: 'innerpage').script(index: 47)
Though hopefully there's something more unique you can use than just the 48th script element.
From there you get the information at the element as desired:
element.text
element.value
element.attribute(attribute_name)

Resources