Chrome - active tabs locations and starting tabs location - python

I was wondering where the Chrome settings file that contains the URLs of web pages to open when you start it next time is. I couldn't find a similar question here. And also note that I am not talking about multiple homepages here, if that is possible though, that is a solution I guess. Chrome has this functionality to open all the tabs that you had open, the next time you start it. Though, sometimes by mistake, you may open a new chrome instance and close the one that you had with multiple tabs (thus, next time it will open the web pages in the second instance; i.e. in the last one that you had open). So I wanted to write a python script that I could execute that allows me to do two things:
1) Save the current state of open tabs
2) Restore that state next time I open Chrome (for an extension, I may also want to append the saved web pages from 1) into currently open web-pages).
This is not difficult, I think. I just need to know, probably, two locations. One - where the active tabs are, Two - where the starting tabs are.
Thanks

Related

how can I read/scrape the firefox task manager (about:performance) from python?

I have tried to do this with selenium, setting my normal firefox profile as the profile, then loaded the firefox about:performance page which should list all the open tabs with memory usage etc., but it only sees the tabs open via selenium. I thought using the same profile would allow me to see tabs opened normally in an already running instance of firefox.
I would like to be able to read the memory usage, energy impact and so on, as displayed in about:performance, for each tab for use in my code. Any help or advice greatly appreciated, thanks. The ideal solution for me would use python, but if anything else can get the result I want, I would be very keen to know about it.

Selenium : Ctrl+click for opening in new tab in a human like way

problematic:
My goal is to do an iteration on a result list and opening each result in a new tab, performing my tests and closing the driver related before beginning again with the next on the list. I am using a firefox webdriver on Ubuntu 18.04
I saw lot's of post similar to the one I am formulating right now but some of them date from 2014, 2015 and the answer seems to be out of date.I learned that selenium changed his policy regarding the navigation in multiple tabs. Plus,** none answer fit with a normal user behaviour. **
Here are the solutions I found on related post:
1) Use send_keys methods such as :
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + 't')
However, it doesn't seem to be working nowadays.
2) use actionsChains with key_down and Key_up:
ActionChains(driver) \
.move_to_element(element) \
.pause(uniform(0,2)) \
.key_down(Keys.CONTROL)\
.click(element) \
.key_up(Keys.CONTROL)\
.perform()
I notice that this combo act like a right click, but it doesn't open in new tab.
Note:Both previous methods were also tried with SHIFT and COMMAND in place of CONTROL for avoiding stupid mistake.
NOTE: This method is exactly the ones for which this question is consider as "duplicate" but it is not working anymore. -> it gives exactly the same result, which is acting as a right click cf : Opening a new tab using Ctrl + click combination in Selenium Webdriver
3) Use the "execute_Script" method for open new windows with js:
new_page_url=element.get_attribute('href')
driver.execute_script("window.open('"+new_page_url+"')")
This solution work, but it is not a normal behaviour for an internet user.
Question
1) As I supposed that those two first solutions are out of date, is there any new way to open a link in a new tab by using the shortcut control and click, like could do a normal user?
2) If I am wrong, and those solutions are still available, why does it give me such unexpected behavior?

Python with selenium: rerun on pre-existing browser

I'm using Python with Selenium 2.44. When the test fails, I can't just uncomment all the code before the failure when debugging it, because the driver will not be declared for the browser. Therefore, whenever I try fixing something, I always have to open a new browser in the test case. This is rather... slow since I have to login, which adds an additional 30 seconds (not devastating, but annoying). I want to know if there's a way for me to just continue a session, or do something that allows me to start the test midway through (so if I have the webpage open already, I can just immediately start clicking things rather than opening a new browser). Is this possible?
For example, if I had something along the lines of:
driver = webdriver.Firefox()
driver.get("google.com")
driver.find_element_by_xpath("//input[#id='gbqfq']").send_keys("cats" + Keys.RETURN)
This should open Firefox, go to google, and search for cats. Pretend like there's a ton of stuff you have to do before you can actually make it to the google page, though. Now if it were to fail on the search for cats, the only way I would be able to test to see if I fixed the code would be to rerun the test (webdriver.Firefox() would open a new browser). Rather than that, assuming I'd still have google open, I'd like the selenium test to just start off on the previous browser and google page (therefore saying the first step in the code would be the send_keys("cats")). Is this possible?
I think that this was a similar question, but it didn't get checked off as answered: How to resume browser session or use existing browser window with Selenium-Python?
This one also seems similar, only pertaining to Java: How do I rerun Selenium 2.0 (webdriver) tests on the same browser?
Thanks.
Look into pdb: https://docs.python.org/2/library/pdb.html
Placing this in your code will stop the progression of the test as is until you tell it to continue in your shell.
Using your code snippit:
from pdb import set_trace
driver = webdriver.Firefox()
driver.get("google.com")
set_trace()
driver.find_element_by_xpath("//input[#id='gbqfq']").send_keys("cats" + Keys.RETURN)
will stop your execution after getting the url, allow you to tinker, and then continue from where the test left off.
Alternatively, while debugging, you can just remove the driver.quit() statement, wherever it happens to be, which will keep the browser open wherever your assertion failed. But if you're using a framework like Django with the LiveTestServer Client, you won't have access to browse the site further. pdb will allow you to keep the test server active.

How to get multiple widows to appear in a slightly different position?

I have this program that stores some information about my books, and this information is shown in windows, that appear whenever I double-click a book title in a list. But there is the possibility of searching for a book using it's title or author, and that can make the program open multiple windows. My problem is, all the windows are opened one over the other, and, until the I move a window, or close it, it seems that only one window was opened. Is it possible to, with python and tkinter, make the windows appear in slightly different positions?
You can use the geometry method of a window to specify where you want it to appear. For example, w.geometry("+100+200") will open the window w at coordinate 100,200. Each time you open a new window, adjust the coordinates appropriately.
Normally, though, this is something the window manager does for you. It's odd that it's not doing this for you.
The geometry method is documented here, among other places: http://effbot.org/tkinterbook/wm.htm#Tkinter.Wm.geometry-method

How to open a New window in Python PAMIE

I am using PAMIE to auto log in to websites and I have a couple to do. I got the scripts down to do this but I can't get PAMIE to open a new IE window so when I run the script it just opens one logs in and then when the next one is open it closes the first and opens the second and so on. So how do I get PAMIE to open new windows. This is what I have..
website="https://website"
ie.navigate(website)
ie.setTextBox("username","Myusername")
ie.setTextBox("password","mypassword")
ie.clickButton("btnSubmit")
Then I want to do this again but need it in new window.
website="https://website"
ie.navigate(website)
ie.setTextBox("username","Myusername")
ie.setTextBox("password","mypassword")
ie.clickButton("btnSubmit")
I tried ie.new before navigate(website), if someone could please tell me what command to open a new window I would appreciate it. I have also trie ie.change.window, and can't get it to work. Thanks
The web page is being re-opened in a single IE instance, because you're only using one PAMIE instance. If you really want multiple IE windows open, you can use multiple PAMIE instances.
Here is one very simple and crude example. Note that your import line may be slightly different than mine.
from pamie30 import PAM30
ie1 = PAM30.PAMIE("http://www.google.com")
ie2 = PAM30.PAMIE("http://news.google.com")
So now you can use ie1.navigate(), or ie2.navigate(), etc. to fill out the forms on your websites.
For example, to use the first IE instance:
ie1.setTextBox("q","my text goes here")
ie1.clickButton("btnK")
And when you're done with an instance you can kill it (it will leave the web page open, if you haven't done anything else with it), this just kills the Python object:
ie1 = None

Categories

Resources