XPATH: Inputting value for an unknown number of xpaths using .find_elements_by_xpath - python-3.x

I've done a lot of searching around for this, but it seems like I can only find the answers in .js . What I would like to do is through python use .find_elements_by_xpath , and having selected an unknown number of elements, input a value by iterating from top to bottom of relevant elements. It is important to know that there may be anywhere from 1+ number of elements that need to be filled.
Some have suggested this method in .js :
driver.findElements(By.xpath("<xpath>")).then(function(elem) {
for(var i=0; i<elem.length; i++){
driver.findElements(By.xpath("<xpath>")).get(i).sendKeys('some Text');
}
});
I'm not skilled enough to translate that properly, but maybe it'll give someone an idea for the solution.
Hopefully my intention is clear enough! Thanks everyone so much for your help.

This is how it will look like on Python. Code with comments:
from selenium.webdriver.common.by import By
from selenium import webdriver
website = "https://YOURWEBSITE.com"
driver = webdriver.Chrome(executable_path='YOUR/PATH/TO/chromedriver.exe')
driver.get(website)
# if you want to find all inputs on the page using XPATH and send there some value use:
all_inputs_on_the_page = driver.find_elements(By.XPATH, "//div//input")
for input in all_inputs_on_the_page:
input.send_keys("TEXT_TO_INSERT")
# if you want to find some elements and after that - find some elements inside these elements then use:
divs_on_the_page = driver.find_elements(By.XPATH, "//div")
for div in divs_on_the_page:
inputs_inside_a_div = div.find_elements(By.XPATH, ".//input")
for input in inputs_inside_a_div:
input.send_keys("TEXT_TO_INSERT")

Related

Access specific index of array with Pug

How can you display a specific item in an array with Pug? For example:
each answer in answers
li!= answer.Response
Will display each item in the array. But, say I wanted just the the third item or, better yet, pass a variable for a specific index to display. What is the syntax for this?
- const indexIwant = 2;
if answers && answers.length>indexIwant
li=answers[indexIwant]
You need to ensure answers is not null and has at least the number of items to include the indexed item you want.
Another thing: don't use != unless you know exactly what data you are handling.
The simplest way to access a specific index of an array in pug:
-const meals = ["breakfast", "lunch", "dinner"]
-const favoriteDishes = ["coffee & doughnut salad","cheese danish soup","red wine","banana split sandwich"]
-const sides = ["ranch dressing","chutney","ketchup","chocolate sauce"]
p I reckon I will fix myself a hefty helping of #{favoriteDishes[2]} for #{meals[0]} with a side of #{sides[2]}.
Considering that indentation and whitespace is everything in jade / pug, this works :))

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.

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)

Check if either of element is located

I wanted to check , if element1 or element2 is present then return true.I use following code to check if element 1 is present.Now I want to check if either element with class name element1 or element2 is present, it should return true and finish the wait condition and move on to next line
driver.wait(until.elementLocated(by.className('element1')), 10000);
Basically something like below ? :P
driver.wait(until.elementLocated(by.className('element1 || element2')), 10000);
My example DOM looks like below
<div class="element1"></div>
OR
<div class="element2"></div>
if anyone of div present, My coditions will be met..sometimes webpage generate only element 1 , sometimes it generate only element2, or sometimes both.
You can do this by using a css selector like this:
by.cssSelector("div.element1, div.element2")
The comma is an OR operator in this case.
Here is the solution to your Question-
In Selenium 3.4.0 to induce ExplicitWait you can use multiple clauses with ExpectedConditions as follows:
Java
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.or(
ExpectedConditions.visibilityOfElementLocated(By.className("element1")),
ExpectedConditions.visibilityOfElementLocated(By.className("element2"))));
Let me know if this Answers your Question.

Gradle: How to filter and search through text?

I'm fairly new to gradle. How do I filter text in the following manner?
Pretend that the output/result I want to filter will be the two URLs below.
"http://localhost/artifactory/appNameIwant/moreStuffHereThatsDynamic"
> I want this URL
"http://localhost/artifactory/differentAppName"
> I don't want this URL
I want to put up a "match" variable that would be something like
variable = http://localhost/artifactory/appnameIwant
So essentially, the string will not be a perfect match. I want it to filter and provide back any URLs that start with the variable listed above. It cannot be a perfect match as the characters after the /appnameIwant/ will be changing.
I want to use a for loop to cycle through an array, with an if then statement to return any matches. For instance.
for (i=0; i < results.length; i++){
if (results[i] strings matches (http://localhost/artifactory/appnameIwant) {
return results[i] }
I am just filtering the URL strings themselves, not anything complicated inside the webpages.
Let me know if further explanation would be helpful.
Thanks so much for your time and help!
I figured it out - I just used
if (string.startsWith"texthere")) {println string}
A lot easier than I thought!

Resources