Selenium xpath each element 5 [closed] - selenium

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I use selenium and xpath selector. I know how to get a certain element. But how to use xpath to get each element 5?

You can always solve it in your language of choice.
Python
For example, in Python, the extended slicing allows to do it rather simply:
driver.find_elements_by_xpath("//table/tr")[0::5]
You can also use the position() function and the mod operator:
//table/tr[position() mod 5 = 0]
driver.find_elements_by_xpath("//table/tr[position() mod 5 = 0]")
Java
List<WebElement> elements=driver.findElements(By.xpath("//tbody/tr[position() mod 5 = 0]"));
System.out.println(elements.size());
for (WebElement element : elements) {
System.out.println(element.getText());
}

Related

Xpath element not found [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I can't find my link using this Xpath //a[contains(text(),'My stuff')]
Though I can find it through DOM :
HTML :
<li _ngcontent-c8="">
<a _ngcontent-c8="" href="my-stuff">
{" "}
My Stuff{" "}
</a>
</li>;
When executed :
try:
my_stuff = WebDriverWait(driver, 8).until(
EC.presence_of_element_located((By.XPATH, "//a[contains(text(),'My stuff')]"))
)
my_stuff.click()
except:
print("not found")
returns not found
There was a modal messing with my selection that needed to be dissmissed.
i will try to analyse the problem and give you most answer
. first : if you have Xpath Helper "google chrome extension" try to put your Xpath there
=> if you find it then the problem in your code . we will try to change it
=> if you can't find it with Xpath helper ,than there is multiple choice:
first try to find if there is a "table" or new "form" above your Xpath , that you can't get it with absolute path
. you must get inside the "table" or "form", then find the path inside it
, tell me if that didn't work

How do I Extract ZIP code from a Website using selenium python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am new to python selenium i want to extract
https://tools.keycdn.com/geo
tools.keycdn.com/geo
I need to extract only postal code i.e 10080
only zipcode not any other text
and print it out on screen
There is a cookies accept button, so you need to accept that first, and then using //dt[text()='Postal code']//following-sibling::dd xpath you can extract the postal code. See below
Code:
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.get("https://tools.keycdn.com/geo")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class*='alert-cookies']"))).click()
postal_code = wait.until(EC.visibility_of_element_located((By.XPATH, "//dt[text()='Postal code']//following-sibling::dd"))).text
print(postal_code)
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

other webelement is also treated as List<WebElement> [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
After accessing the webelements of List. Other webelement is also treated as List webelement.
List<WebElement> BrandTerms =driver.findElements(BrandTerm);
js = (JavascriptExecutor) driver;
for(int i=0;i<=1;i++)
{
js.executeScript("arguments[0].value='"+Bandtermsvalue+i + "'", BrandTerms.get(i));
}
js=null;
driver.findElements(By.id("btnAddBrandedTerms")).click();
Why is this webelement is treated as list element with message "click() is undefined for the type List"
alternatively You may extract and click on the first found
List<WebElement> BrandTerms =driver.findElements(BrandTerm);
js = (JavascriptExecutor) driver;
for(int i=0;i<=1;i++)
{
js.executeScript("arguments[0].value='"+Bandtermsvalue+i + "'", BrandTerms.get(i));
}
js=null;
driver.findElements(By.id("btnAddBrandedTerms")).get(0).click();
OR
driver.findElement(By.id("btnAddBrandedTerms")).click();
Please see webdriver API specs
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/remote/server/handler/FindElement.html
and
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/remote/server/handler/FindElements.html
Also highly recommending to double check selenium top tips and tricks and try that out in your project.
Hope this helps.
findElements returns a list of all the elements which match given xpath. Whereas, findElement returns first matching element using the xpath. But, be careful as if there is no element matching the given xpath, you may face exception.
To solve the issue you are facing, instead of below line:
driver.findElements(By.id("btnAddBrandedTerms")).click();
Please use this line of code:
driver.findElement(By.id("btnAddBrandedTerms")).click();
Hope it helps.

WebDriver:: getText() fetches only partial String [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to read the below html code using getText() {Selenium-WebDriver}, this is my locator and script line
#FindBy(xpath="//p[#class='radiobutton']") WebElement groupMsg;
System.out.println("This is the group message"+groupMsg.getText()); //msg display on console
o/p on console :: This is the group messageRadio button 'Male' is checked
As i showed its showing only "Sex : Male". not getting the "Age group: 5 - 15". Its happening because of (i think Pl correct me if i am wrong)
<p class="groupradiobutton">
Sex : Male
<br>
Age group: 5 - 15
</p>
Can any one give solution for this?
Below code is workig.
String a=driver.findElement(By.className("groupradiobutton")).getText();
System.out.Println(a);
This is printing the output as below in console.
Sex : Male
Age group: 5 - 15
I have tested with the html you have given.
Use below code
driver.findElement(By.xpath("//p[#class='groupradiobutton')).getText();

Robot Framework: How to clear the text in Textfield [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to know that how to remove the text from Textbox. I have did much search from Google but nothing found currently. Please tell me any Keyword which can help me for remove/clean text in field.
You can use this also for clear test field..
driver.findElement(By.id("textfieldid")).sendKeys("");//empty string
or
Input Text (your web element locator ) ${empty}
You don't need a Robot to do it.. Just use Selenium.
driver.findElement(By.id("username")).clear();
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#clear()
You don't need it. Check here
def input_text(self, locator, text):
"""Types the given `text` into text field identified by `locator`.
See `introduction` for details about locating elements. """
self._info("Typing text '%s' into text field '%s'" % (text, locator))
self._input_text_into_text_field(locator, text)
The Input Text keyword is calling the
_input_text_into_text_field
which is anyway sending the clear command:
def _input_text_into_text_field(self, locator, text):
element = self._element_find(locator, True, True)
element.clear()
element.send_keys(text)