How to make an assertion to find xpath? - python

Bear with me, I am very new to this. I am writing in Python using Selenium Webdriver.
I have a test written out and I want to make an assertion to find is a class is present. I have tried multiple ways to go about to do this however i feel as though it is not working correctly.
I have attached a photo of the code I am looking at (the highlighted line) and this is what I have so far. This fires which is a pop up to notify a success the form was submitted.
self.assertTrue(driver.find_element(By.XPATH, '//*[#id="message-center"]/div'))

I would take advantage of the .find_elements method. It will return an empty list if no such elements are found.
self.assertTrue(driver.find_elements(By.XPATH, '//*[#id="message-center"]/div').count() > 0);

Related

Selenium "sendkeys" does not work with a particular website but manually typing it in works

https://phytozome-next.jgi.doe.gov/blast-search
on this website if i use the blast tool with the sequence "MARPFTLSLLSLCLLLSAWSCFGGSSSTNRFNICQLNSLNALKPDHRVETDGGLVETWSSRHPELECAGVTVTRRTLYRNGFQMPSYSPYSQMIMAIQGKGALGLALSGCAETYEEPAKESSSSSQKPSDSHQKIRQFDQGHVMLIPRGVPFWIFNTGDEPLITVTLLDTSSEDNQLDQSPREFYLAGNPDIEHPEAMKEKQQQQAEEEGGNVLSGFGKRFLARALNIDQDTANKLISPDDEMKQIVKLKEGLSVISPKWQGQQEDEDEDDDDEDEDESVSRPSRRPSHGKRVHKEEETVVEPYPHGKHVHKEVEKEVEPLPPRKHVHKEEEKEIEPLPPRRSRHHHDEGEDEGEEEEKPRARRTRGPTPSPKGEGHRGVEEEDESEDTKGHKTRHEKTRHEKSWKEHRPEGEDVEKGEAHEEWETRPSKDKPHGSNGLDETICSSKLQFNIARPKGADFYNPKAGRIKNLNSQSLPALQHFGLSAQYVVLYKNGIYSPHWNMDANSVIYVIRGQGQVRVVNNEGIVMFDDELKKGQLLVVPQNFMVAEEAGDQGFEYVVFKTNDNAVTSYLKETFRAFPAEVLVNIYKLKHSQVHDLKYNGNLGPLVNPENSLDQSS"
along with Archaeplastida selected in order to return all results i get 100 results
when i use selenium with the chrome driver the web page informs me that "The Phytozome service couldn't get what you were after."
i have narrowed the problem down to something involving send keys, since if i run selenium all the way up to the point where it sends keys and i input the keys myself it works just fine.
When you say you "input the keys yourself", are you really typing those hundreds of random-looking letters yourself, one at a time? That looks about impossible.
It totally depends on the particular website's programming, but it could be a timing issue. You can try a separate SendKeys() call for each character with a pause between each one.
i am not sure what the problem was but i simply used execute script instead of send keys. that posed another issue, which i resolved by sending a space key and then clicking backspace.
i tried everything including sending the keys one by one with a space in between and this was the only thing that worked.

Python, Chrome Task Manager - Programmatically Access Chrome Task Manager's Text WITHOUT USING CHROMIUM

So, as many have encountered Chrome's memory usage can be debilitating so I want to PROGRAMMATICALLY access "Chrome's Task Manager" or something equivalent which displays Tabs, their PIDs, AND SUBFRAMES (if possible) so I can create a script to kill them. After a ton of reading, and a lot of answers suggesting that it might not be possible and some where the posters didn't answer the question, I'm just not sure that the answer to THIS question has truly definitively been given yet.
The closest I've gotten is using "chrome://system/" in the omnibox, but when you expand "mem_usage" it doesn't give you useful memory usage numbers per tab at all and lacks the subframes, but with this method I'd planned to use the tab titles to kill the tabs if I could have actually gotten their memory footprint.
I tried using the win32gui's FindWindowEx method to get the text, but that requires you to know the child element of the text displayed so I downloaded "Window Detective" to see the name of the element(s) the "Chrome Task Manager" displays, the PIDs, and Tab names for, but "Window Detective" only came back with the "EmbeddedMenuWindowClass" which is the entire Window, not related to the text displayed.
I made a fleeting effort to use Selenium, but it seems as though it's just not capable of grabbing Special Menus inside of Chrome over just grabbing tab names and elements within a webpage.
I also tried "pywinauto" but that thing's documentation is really, really bad. I've tried using the "connect" method, "hwndwrapper", etc. but grasping what parameters to use, how their being passed, and what to expect along with how to then use the output you get (such as a list of PIDs or Handles) is seriously bungled. I would have loved if the documentation included the OUTPUT so I could actually see what I'm supposed to get so I don't end up rabbit-holing only to find out that everything I've tested doesn't deliver what I'm looking for. Sorry if that sounds whiny, but I spent hours reading the PDF and the official docs and got badly frustrated.
I also tried launching chrome via the Command Line following this document for logging (http://www.chromium.org/for-testers/enable-logging), but it may be a Chromium only thing, but cannot find the logging document that's created and when I redirect its output to a file, I get nothing useful.
But yes, does anyone know how to programmatically access "Chrome Task Manager's" text, or something else that will deliver similar output? Thank you.
Why to just to use the python os module something like this:
import subprocess as sub
p = sub.Popen(['your command', 'arg1', 'arg2', ...],stdout=sub.PIPE,stderr=sub.PIPE)
output, errors = p.communicate()
print output
You can use the command ps to list all the proceses running in chrome however that only will give you the pid not the name of tab to get that I think the only way is to get the data directly from chorme by creating an extension.
https://developer.chrome.com/extensions/tabs

Finding screen elements in appium(iOS) using contains

I am doing QA and recently started using appium and cucumber to automate some tests and am still new to this.
I succeeded in what I wanted to automate using some functions like this one.
def function(element_name)
find_element(:xpath,
'//XCUIElementTypeOther[#name="' + element_name + '"]'
).click
end
This works for what I want, but now I am trying to redo the same functions but using contains. Something like this
def function(element_name)
find_element(:xpath,
'//*[contains(text(), element_name)]'
).click
end
What I'm getting is
An element could not be located on the page using the given search parameters.
I think I am just not using contains the right way but I am really not sure.
Xpath is not a good way for searching elements in Appium/XCUITest as it is often too slow and might not work as you expect it to work (your case with contains).
Instead you should you try XCUITest native locator strategies: iOSNsPredicate or iOSClassChain, e.g.
driver.find_element_by_ios_predicate("label contains 'Some text'")
You can check more examples here: python client tests, java client tests
the way you are using contains is correct, the only problem is its not finding the element.
If XPATH is not working , why dont you try with relative path e.g. below
def function(element_name)
//tbody//td[contains(text(),element_name)]

Selenium - what is the difference between click and clickAndWait?

I am new to selenium and need some clarification on some stuff. I've tried click and clickAndWait in the IDE, and while the references stated clearly what each meant, when I export the test case to Python, it seems like both are doing the same thing here
driver.find_element_by_xpath("//li[#id='pa-u_8298348-bd']/a/span[2]").click() #click and wait
driver.find_element_by_link_text("IMVironments").click() #click
Can someone tell me what is the difference here then?
From Selenium docs:
Many Actions can be called with the "AndWait" suffix, e.g.
"clickAndWait". This suffix tells Selenium that the action will cause
the browser to make a call to the server, and that Selenium should
wait for a new page to load.
2 years later, I know. But I was looking for the same answer and found it.
From the Selenium Wiki
The AndWait alternative is always used when the action causes the browser to navigate to another page or reload the present one.
In other words, Selenium's webdriver, when utilizing the Click() command, will inherently use the "AndWait" modifier if it recognizes a page load.
See also Interface WebElement
void click()
Click this element. If this causes a new page to load, this method will attempt to block until the page has loaded.
I'm not sure if this is a python thing, but it appears you are only calling 'click' and are not calling 'clickAndWait'. I think this is what Trott is pointing out too.
With this said I have noticed that IE often requires AndWait a bit more often than Firefox or Chrome, which seem to deal with just click a bit better (due to speed?). You can also create your own 'wait' in Selenium, which is what I usually do in critical situations (as in you need to interact with a specific element so you 'wait' until it exists).
EDIT:
After your comment, I now understand what you are saying (was mildly confused about the 'clickandwait' comment :P). From what I can tell AndWait is not always available for the Python binding, I found that here, but I'll admit it's not 100% clear as it suggests otherwise in other places. Perhaps this would be useful instead?
wait_for_page_to_load(timeout)[source]
Waits for a new page to load.
You can use this command instead of the “AndWait” suffixes,
“clickAndWait”, “selectAndWait”, “typeAndWait” etc. (which are only
available in the JS API).
Placed this as an edit, because was too long for a comment, sorry!

sel.click("xpath=//*[#id='seriesNwsHldr']/div[2]/p[1]/a") is not working

Exception: ERROR: Element xpath=//*[#id='seriesNwsHldr']/div[2]/p[1]/a not found.
I checked in Fierbug. The path is correct but I don't know what's the reason for this test case to fail.
It looks like a problem of timing. May be you can intentionally add wait time till the element appears on the page.
Another possibility is that element which you are trying to interact is hidden.
Would be great if you can post errors you are getting when you test fails.
Can I have the site for checking?
BTW sometimes you should to wait the loading of the page, so you need to do before of this action an instructions like:
clickAndWait(30000)
in my cases it solves a lotof problems :)

Categories