python xpath doesn't seem to work for some elements - java

I'm trying out Selenium. I've built a quick script using the Selenium IDE. It logs in to a page, opens up a 'Search' window, does a search, and then opens up one of the results.
When I run the test in the Selenium IDE, it works fine. When I export to a Java JUnit 4 test case, it works fine. But when I export to a Python 2 unittest, the xpath searching seems to be very buggy...
Example:
The page has an input element with name='keyword'. When I search using java with driver.findElement(By.name("keyword")); it's all good - it finds the element.
When I search using python with browser.find_element_by_name("keyword"), it can't seem to find it. I've also tried browser.find_element_by_xpath("//input[#name='keyword']") and browser.find_element_by_xpath("/html/body/form/div/ul/li[2]/input"), the second of which is the xpath I get when I use firebug and the Copy XPath feature.
I've tried adding a delay to the python code before it searches for the input, but to no avail.
Does anyone have any idea why the python calls can't seem to find the element? This seems to happen with different elements throughout the pages of the web app (but doesn't happen in the Selenium IDE that I've found so far)...
I appreciate any help you guys can provide!
Edit:
The error showing in the console is:
Traceback (most recent call last):
File "test.py", line 37, in <module>
elem = browser.find_element_by_xpath("/html/body/form/div/ul/li[2]/input")
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 213, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 671, in find_element
{'using': by, 'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 156, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 147, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"xpath","selector":"/html/body/form/div/ul/li[2]/input"}'
Cheers
Jarrett

Turns out, the website was opening a new window when a link was clicked, but the driver was still referencing the old window. Not sure why the Selenium IDE export didn't catch that.
Anyway, I had to set the window for the driver to the newly opened window, and then it worked ok.
Hope this might help other people.
Cheers
Jarrett

check if its inside an iframe, you need to go to that iframe if it does.
driver.switch_to.default_content()
driver.switch_to_frame("fameid")

Related

How to see the directory of a file in Intellij debugger?

This may apply across languages in intellij, though my example is about Java. I have a breakpoint at the line after this one:
currentOut = new PrintStream(new FileOutputStream("tmp.output"));
The variables tab shows:
I have navigated down charOut, out, and testOut - nowhere is a path to file tmp.output shown. In the debugger, how can I see where this file got created?
It will be in the current directory of the Application.
You can get it in the debugger by executing this inside evaluate expression box:
new java.io.File(".").getAbsolutePath()

Selenium ChromeDriver use custom profile folder - permission error

so I'm trying to create a custom webdriver (as in (down)load the normal driver but use custom profile settings) to get it to open any tab with a popped-out DevTools window (set to "Console").
I figured I could do that by setting up such a preference file by just interacting with my own installed Chrome version and copying my preferences to another folder, then let the Selenium WebDriver load its preferences from that folder.
But when I tried starting the WebDriver using this:
options.addArguments("--profile-directory=\"Default\"");
options.addArguments("--user-data-dir=./temp-selenium");
... it always shows an error (in german, roughly translated to):
Google Chrome can't read or write in the following directory: ./temp-selenium
Removing the second line completely just starts a Chome window with a user profile selection prompt. Setting up a file path to something like AppData doesn't work because then I get the following error:
Could not remove old devtools port file. Perhaps the given user-data-dir at "myFilePath\User Data" is still attached to a running Chrome ...
By the way, why I'm doing this: I just want to SEE the console logs while testing stuff. Can't imagine it's actually THIS difficult to get to it. My tests are way too fast to manually open the console in time and I need to find a bug that happens every 100 tries. Opening the console 100 times is just way too tedious.
Anyone have an idea?

The output window in vs code is not editable, so how we can type the input when the program asks for it?

The Error is shown when I input anything in the output window
As the name implies, output window is only for displaying output. It seems that you're using VSCode Coderunner extension, which by default runs program in output window.
To change it to terminal, simply put :
"code-runner.runInTerminal": true
In your VSCode User Settings (shortcut: Ctrl + ,).
In general, this problem seems to occur in code runner. But when I was Doing settings according to the upper Most Upvoted Answer, I was not able to edit Json File Correctly as It's showing some error. So, If this is the case, You can do this too.
GO TO VS CODE SETTINGS.
IN USER SEARCH BAR, Type CODE RUNNER TERMINAL
Now, Check the option of Run in Terminal. Voila!! Good To Go!
Solution Image
Type the input in the console, not the output window.
you cant input anything in output window.
if you want make any change or edit in output section you should first do debug in console box and then you can make any change you want
Use the shortcut command Shift + Ctrl + X to bring out the extensions side bar,
Now in the search extensions in market place entry input "#installed code runner",
Make sure you have installed code runner extension (If you haven't do that now),
Next right click on the extension and select extension settings,
Scroll down to run in terminal, finally select the check button.
That should do it.
The output window is only for outputs. Use your terminal for input-based code.
To get output in terminal of vscode :
step1:
Click Ctrl+, or open the settings
step2:
search for code runner
step3:
turn on the Run in terminal checkboxscreenshot is included

Java File can't open certain files

I'm trying to open files from within Java with something like this:
java.awt.Desktop.getDesktop().open(new File("c:\\coolfile.txt");
Of course it all works fine and dandy in most cases.
HOWEVER!
When I have a file with the unicode character u3000, I cannot open it! Even if the file exists.
For example:
java.awt.Desktop.getDesktop().open(new File("c:\\coolfile\u3000withweirdname.txt");
I get an Exception, EVEN WHEN THE FILE EXISTS
[java] java.io.IOException: Failed to open file:/E:/_prog/test%E3%80%80.txt. Error message: The system cannot find the path specified.
Please help me i tried pretty much everything. This is driving me insane :/
Edit:
To give some more info:
I can easily create file with this name from Java.
It seems it has something to do with whitespace
I don't know if it applies to other characters; I didn't find any yet. But of course if there's 1 there could easily be 100.
I'm pretty sure I can't read from the file or write to it from Java, but I haven't tested that since it isn't my main concern.
java.awt.Desktop.getDesktop().open(new File("c:\\coolfile\u3000withweirdname.txt");
That doesn't compile. Clearly it isn't your real code.
[java] java.io.IOException: Failed to open
file:/E:/_prog/test%E3%80%80.txt
And there is proof. Clearly you passed a URL to new FileInputStream(). It doesn't take a URL string, it takes a file name.
Ok i think i actually found a kind of solution to my question and i post it here to help any people that may have similar problems.
This fix only works for Windows (XP and up i think) BUT i don't even know if this problem exist in other OS. And even if it does a similar fix should be possible.
I am using the following code to succesfully open a file with the character:
Process p = new ProcessBuilder("cmd", "/c start \"\" \"E:\_prog\test\u3000.txt\"").start();
Which opens the file 'E:_prog\testu3000.txt'
As far as i know \u3000 is ideographic space character. To test your code I created a file with name CompanyAlt+3000Address.
Note: when you press Alt+3000, windows will create an ideographic space character. Then I copied the file name, to my java program and it worked for me.
Desktop.getDesktop().open(new File("C:\\Users\\Chandru\\Desktop\\Companyâ••Address.txt"));

Eclipse autocompletion problem

Just installed Eclipse Helios (Win7 64) and I'm having a strange problem. I type syso-Ctrl-Space which is expected to complete to System.out.println("") but it doesn't work! I searched the web for about an hour now, I set Preferences > Java > Editor > Content Assist to defaults, nothing. Everything else seems to work. Ideas?
To be clear: the shortcut works, it completes everything except "syso" as seen so far, on syso (also tried sysout) it says "No Default Proposals". I couldn't find this kind of shortcuts in the Content Assist, but I set them to default anyway.
If you still cant get auto-completion working,try enabling the Template proposals under Content assist menu.This is the exact location,
Windows->Preferences->Java->Editor->Content Assist->Advanced
And make sure you have only one entry for syso(Under Templates menu).
I believe the right template is "sysout" and I believe this can be configured somewhere but could not tell you where. Try sysout and then Ctrl+Space
It's a template, you can find all templates under
Window -> Preferences -> Java -> Editor -> Templates
There you can see that the template name is "sysout" this will then trigger the string substitution for System.out.writeln() but you can change both the template name (if you prefer syso) and/or the actual code produced by invoking the template
Cheers
I am using Eclipse Galileo and had the same problem. Even after trying the above proposed solutions but it didn't work. After making the below change it finally comes out.
Go to Windows->Preferences->Java->Editor->Templates
Select sysout template and edit it.
Change the context from Java statement to Java.
And it worked for me. I hope this will be helpful for you.
As Peter stated, it is under Window -> Java -> editor -> templates.
Look for the line with the following values.
sysout "Java Statements" "Print to standard out" "on"
the preview should be as follows...
System.out.println(${word_selection}${});${cursor}
Your cursor needs to be located in a place where it would be legal to get that line. If your cursor is within a method definition and you type syso you will get the replacement. However if your cursor is in the portion of the text outside of any method definition, say where you declare variables for your class for instance, then syso will not suggest the illegal line system.output.println();.
I had this problem, read all the answers here, became convinced that it MUST be working in my copy, and realized I had been typing syso BELOW the closing } of my last method instead of in the method.
Eclipse on!
You may try to set the shortcut of Content Assist with ALT+L, if it works, then you can choose your favorite shortcut again. (The shortcut Ctrl+Space conflicts with the shortcut of switching input method). In eclipse on linux, the shortcut of Word Completion is ALT+/, so if you set this as the shortcut of Content Assist it also conflicts.)
Just go to the below address:
Windows->Preferences->Java->Editor->Content Assist->Advanced
and click restore default..........that's all
its work like a charm again.
Also check in this eclipse menu:
Windows -> Preferences -> Java -> Appearance -> Type Filters
If you see java.* or javax.* here then try removing it (or Restore Default will do the job) and see that works, it did for me.
Just found the solution for Eclipse Luna: write syso and press CTRL+Space and click on "enable intelligent code completion" and you are set.
I am sure you can find it somewhere in the options as well but this way it is fast and easy!
DIRTY WORKAROUND:
You can try out the following AutoHotkey-Code if you're still struggling like I am (STILL haven't been able to fix it myself)
#persistent
#ifwinactive ahk_class SWT_Window0 ; eclipse java
:*:syso::
sendraw System.out.println()`;
loop, 2
send {left}
return
#ifwinactive
You need to write this in a .ahk file and run it (AutoHotkey needs to be installed). You might wanna put this file into your startup folder. It's a very useful tool, you might wanna expand it one day.
You might have overlapping shortcuts.. Goto 'Overview' than the 'Keys' subsection. Type "ctrl +space" in the 'filter text' section. You should see the "content assist" and probably another command with the same shortcut. If another shortcut does exists, change its shortcut to something else.

Categories

Resources