I'm wondering, I'd love to find or write condition to check if some element exists. If it does than I want to execute body of IF condition. If it doesn't exist than to execute body of ELSE.
Is there some condition like this or is it necessary to write by myself somehow?
By locating the element using xpath, I assume that you're using Sselenium2Library. In that lib there is a keyword named:
Page Should Contain Element which requires an argument, which is a selector, for example the xpath that defines your element.
The keyword failes, if the page does not contain the specified element.
For the condition, use this:
${Result}= Page Should Contain Element ${Xpath}
Run Keyword Unless '${RESULT}'=='PASS' Keyword args*
You can also use an other keyword: Xpath Should Match X Times
I prefer using Get Matching XPath Count since the accepted answer throws error if element is not found
${count}= Get Matching XPath Count ${Xpath}
Run Keyword And Return If ${count} > 0 Keyword args*
I also had the same doubt, but above answers doesn't satisfy what I wanted to
so,I used rpaframework library
*** Settings ***
Library RPA.Browser.Selenium
*** Tasks ***
Verifying if a link is present or not
Open Available Browser https://www.amazon.com
${val} Does Page Contain Link //a[contains(#href,'some_text_present_in_your_link')]
IF ${val}== True
Log Link is present in the web page.
ELSE
Log Link is not present in the web page.
There are many keywords present in this library to verify other things like elements, button, checkbox, image, textfield etc.
These keywords return True or False, so we can perform other steps based on variable value.
Note: Do not forget to install the rpaframework library.
pip install rpaframework
Related
Hi im trying to navigate from page 1 to page 5 (the element can be in any of the pages)and find and click on a specific element using python selenium.
The following is the element from the page:
<span _ngcontent-mtx-c123"" class"ng-star-inserted">ABC Company</span>
i tried by using : driver.find_element_by_name("ABC Company").click() but this doesnt work.
Another Way i tried:
1. element_path="//span[contains(text(),'ABC Company')]"
2. while True:
3. if(driver.find_elements_by_xpath(element_xpath)): driver.find_element_by_xpath(element_xpath).click()
4. else: driver.find_element_by_xpath("xpath to goto next page").click()
I need the code to find element from the next pages until its found and then click it.
is there any other way to do this???
Thanks in Advance
First, you need to check if the element is present and only if it does - click it. Otherwise you will get exception while trying clicking non-existing element.
driver.find_elements returns a list of web elements matching the passed locator. So if there are such elements it will return non-empty list interpreted as True by Python. Otherwise empty list is returned interpreted as False.
As about the locator for the element you are looking for: you can locate the element according to the text it contains. It can be done with XPath.
As following:
element_xpath = "//span[contains(text(),'ABC Company')]"
if(driver.find_elements_by_xpath(element_xpath)):
driver.find_element_by_xpath(element_xpath).click()
If you need to update the XPath locator dynamically you can pass the text as parameter. Let's say you have a list of texts, you can iterate on them as following:
for txt in texts:
element_xpath = "//span[contains(text(),'{}')]".format(txt)
if(driver.find_elements_by_xpath(element_xpath)):
driver.find_element_by_xpath(element_xpath).click()
Child element does not have proper xpath or id. But it has text attribute with value "Hi'. Xpath does not contain any expected text. It just contains hierarchy of elements and their positions. So I have used ui_automator with text.
1. child_ele = self.driver.find_element_by_android_uiautomator('new UiSelector().text("Hi"))
2. parent_ele = child.ele.find_element_by_xpath("..")
The above code throws "Element not found exception in line 2"
I have used python debugger to try several combos of xpath. So, it's not even related to explicit wait to assume if it is loading related.
Tried this in Appium Desktop 1.17.0
Please suggest any working solution.
Thanks!
I want to find element based on it attributes.
I have already tried searching by all divs, and specify by attributes, and even searching by *. None of this was solution.
Whole element looks like this:
<div class="charc" data-lvl="66" data-world="walios" data-nick="mirek">
This is my search expression:
driver.find_element_by_xpath('//div[#data-world="walios"] and [#data-nick="mirek"]')
I would like to find this element using python with selenium, and be able to click on it.
Actually I am getting the error
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[#data-world="walios"] and [#data-nick="mirek"]' is not a valid XPath expression.
What am I doing wrong?
The error message is correct because your predicate(s) is/are not correct.
Try putting the predicate in one [...] expression:
driver.find_element_by_xpath('//div[#data-world="walios" and #data-nick="mirek"]')
driver.find_elements_by_xpath("//div[#data-world="walios" and #data-nick="mirek)]")
or
driver.find_elements_by_xpath("//div[#data-world="walios"][#data-nick="mirek)]")
The multiple conditions for selecting the tag can't be within nested []. Either you have to specify within one [] or within multiple []s.
XPath axes methods:
These XPath axes methods are used to find the complex or dynamic elements. Below we will see some of these methods.
XPath expression select nodes or list of nodes on the basis of attributes like ID , Name, Classname, etc. from the XML document .
What i need is to find all the links on the page that have some keyword inside the link itself. So , based on some stack topics i build my xpath as follows:
response.xpath('//a[contains(#href,'/Best-Sellers-Health-Personal-Care')]')
which should return a link like = "https://www.amazon.com/Best-Sellers-Health-Personal-Care-Tongue......"
But i get Invalid syntax error all the time. What am I mistaken?
So what i do now is just add the if contains check when iterating through the list. But hoped there were more elegant and fast solution.
This is because of inconsistent quotes usage.
Just replace
'//a[contains(#href,'/Best-Sellers-Health-Personal-Care')]'
with
'//a[contains(#href,"/Best-Sellers-Health-Personal-Care")]'
I have multiple questions inside which there are more than 1 options.
After selecting the required question element as question_element
I am unable to get the first text box inside this element. I used
question_element.find_elements_by_xpath("//textarea")
but it gives me list of all the elements with tag textarea in the whole webpage. I tried
question_element.find_elements_by_xpath("/textarea")
question_element.find_elements_by_xpath("./textarea")
but they didn't give any results. How do I get the first element with tag name textarea inside the question_element
There are two variants that work for search within already found element (not within the whole page):
question_element.find_elements_by_xpath(".//textarea")
Try like this
question_element.find_elements_by_xpath("//textarea[position()=1]")