Access Dropdown menu on webpage with Python - html

I am trying to write a Python script that accesses a webpage (http://www.yeastgenome.org/locus/S000001142/overview) and downloads a (DNA sequence, fasta) file. The file is automatically downloaded once one clicks on a dropdown menu.
This is the dropdown menu:
Download (.fsa)
and one of the options would for example be:
Genomic DNA +/- 1kb
Could someone please point me in the right direction, how to do this?
The Selenium modules?
Thanks a bunch!!

You basically want to navigate to the page, click on the dropdown to open it, and then click on the selection you want. You will need a couple waits... one to wait for the bottom of the page to load and show the dropdown and another short pause to wait for the dropdown to open.
from selenium.webdriver.support import expected_conditions as EC
driver.get("http://www.yeastgenome.org/locus/S000001142/overview")
wait = WebDriverWait(driver, 4)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a.dropdown"))).click()
option = "Protein" // change to the desired option in the dropdown... must be EXACT text
wait.until(EC.element_to_be_clickable((By.XPATH,"//ul[contains(#class,'open')]/li/a[text()='" + option + "']"))).click()

Related

Unable to retrieve ID of dropdown menu as the page reloads when I try to inspect elements

Below is a screenshot of a webpage that I am trying to write a Cypress test for.
As you can see, I have managed to write "teladoc" into the input box, but I now need to click the dropdown menu to navigate to a different page.
I am not able to get the ID, etc. of the dropdown menu.
When I try to inspect the dropdown menu, the page reloads and the dropdown disappears.
Does someone know how I can inspect this? I tried through the cypress explorer too, but it reloads in that beforehand too.
Since the locator is not there, You can play around with Keypress. The idea is when you have typed and there is the suggested list, you can first press the down key and then enter key. This might result in flaky tests as you don't know how much time does it take for the suggested item to appear.
cy.get(selector).type('{downarrow}{enter}')
You can also directly use contains. If this works this way the tests won't be flaky. You can play around with the timeout value.
cy.contains('Teladoc Health Inc - United States', {timeout: 5000})
.should('be.visible')
.click()
Open the browser console and type this
setTimeout(() => { debugger; }, 5000)

Jump to specific cell in google spreadsheets macro

I am aware of f5 to open the jump to cell dialog, the problem with it, is that, while it allows you to do it once, the next time you press f5, to jump to another specific cell, the previous text is not highlighted, therefore you need to get you hands off the keyboard and select it with the mouse.
If I could have a way where I don't lift my hands from the keyboard it would speed up my workflow tremendously.
Any ideas?
Like, I'd need a macro to pop up the goto range dialog AND select whatever text is there so that I can just type a new cell range (e.g: a391)
Thank you!
There is a simple approach to add customized macros to Sheets. You only need to create a bounded script beforehand that contains a function with your desired actions. Then you can follow these steps:
Go to Tools > Macros > Import
Find your function and click on Add Function and click on the X to close.
Go to Tools > Macros > Manage Macros
Assign a number to your macro and click on Update
Run your macro using Ctrl + Alt + Shift + {NUMBER}
Following those steps you will create a customized macro without difficulty, but feel free to ask me any doubts.
Work around for MacOS (High Sierra, probably it will work on newer os as well)
I downloaded the file AppleScript Toolbox.osax from here: https://astoolbox.wordpress.com/
Put the file in the folder: /Users/me/Library/ScriptingAdditions
Run Automator and start a new Service:
Paste this script
activate application "Firefox" -- or "Safari" or "Chrome", etc
set mousePointLocation1 to {1700, 250} -- coordinates depend on your screen size
AST click at mousePointLocation1 number of clicks 1
And save:
Go to Preferences > Keyboard > Services and add shortcut for this service
After that you will see the new shortcut here:
In Google Spreadsheet I press F5, to show the panel and then I press ^F5 and cursor jump on this panel and does click.
I'm a noob in macOS I'm sure it can be done better. It's just how I managed to do it right now.
Another modification of the script could be more convenient:
activate application "Firefox"
tell application "System Events"
key code 96 -- this is F5 key
end tell
delay .5
AST click at {1700, 250} number of clicks 2 -- or 3 maybe
This way you don't need to press F5. Just press ^F5 and go ahead.
It works
Just in case. If you know Python. I've managed to press F5 move and click mouse with Python module pynput:
from pynput import mouse, keyboard
k = keyboard.Controller()
k.press(keyboard.Key.f5)
k.release(keyboard.Key.f5)
m = mouse.Controller()
m.position = (1700, 240)
m.press(mouse.Button.left)
m.release(mouse.Button.left)
m.click(mouse.Button.left, 2)
https://pynput.readthedocs.io/en/latest/
It can be converted into macOS Service this way:
And then you can run this Service the same way via Keyboard > Services > Add Shortcut
Keyboard shortcuts in Google Spreadsheets are rather suck. If you're on Windows I'd advice to try Autohotkey. It can move cursor at any coordinates on your screen (coordinates can be set relative current window). For example 'Win+F5' could work as F5 + move cursor at top-right corner of the screen and perform a left click.
I just found another a much more obvious native solution.
Put the code in Script Editor:
function jump() {
var range = SpreadsheetApp.getUi().prompt('Input cell').getResponseText();
SpreadsheetApp.getActiveSheet().getRange(range).activate();
}
Save it and add the function as a macros:
Then assign some shortcut to the macros:
After that you can press the shortcut (CmdOptionShift5 in my case), input cell address, press Tab and Enter and here we go.

How to retain the previous state of Viewer Toolbar

I have one custom toolbar buttons for the viewing 2d files. Clicking on same opens the panel for selecting file for comparison and loads the 'PixelCompare' extension. Once the operation is done, and when I click the 'Exit Comparing' toolbar button of PixelCompare, it doesn't show that extra custom toolbar button. Please suggest what is required. How can I handle the inbuilt extension unload. Thanks!

How to find out the link of an element id

I am trying to make a youtube downloader. This works via the webbrowser , so for example the user types in the link of the youtube video in my textbox1 and the webbrowser redirects to "mp4.ee" and inserts the link and then it automatically clicks download. However once you click download on mp4.ee, another download button appears. And here lies my problem, if I click on the second download link it will ask me to save the file however I want to find out the link of the second download button since I want my program to download the file automatically with it's downloader.
My codes:
Code to insert link into mp4.ee and press download:
WebBrowser1.Document.GetElementById("Form").SetAttribute("value", TextBox1.Text)
WebBrowser1.Document.GetElementById("Button").InvokeMember("click")
Application.DoEvents()
With this code it click the second download button which then asks you to save the file however I want to find out the link this button contains in string format:
WebBrowser1.Document.GetElementById("PopupWindowButton").InvokeMember("click")
You can use JQUERY.
$("#Form").val(""+value); //get value
$("#Button").click(function() // event click of button
{});
see this try.jquery.com functions

Opening new tabs in Chrome using Watir causes problems using open tabs

Short version: new tabs in Chrome prevent old tabs from being used, fixing that means that opened tabs with PDFs in them get reused before a human can examine the PDFs.
Long version:
Originally it worked like this:
open new Chrome window to main page of the app (tab #1)
2.[do process A and then] click the button and a new tab (tab #2) opens with PDF A in it.
Go back to tab #1 [do process B and then] click the button and a new tab (tab #3) opens with PDF B in it.
Go back to tab #1 [do process C and then] click the button and a new tab (tab #4) opens and Word document C document gets downloaded.
Go back to tab #1 [do process D and then] click the button and a new tab (tab #5) opens and Word document D document gets downloaded.
All tabs stay open and PDFs can be viewed. Not perfect, but workable.
But then things changed. I pulled frequently used stuff out, put them in methods in another file so all of the various tests could use them, which seemed like a good idea. But that seems to have caused problems with losing focus on the original window. (I may be wrong) now I'm stuck with:
open new Chrome window to main page of the app (tab #1)
[do process A and then] click the button and a new tab (tab #2) opens with PDF A in it.
Stay in tab #2 [do process B and then] click the button and a new tab (tab #3) opens with PDF B in it. PDF A is now lost
Stay in tab #3 [do process C and then] click the button and a new tab (tab #4) opens and Word document C document gets downloaded. PDF B is now lost
Stay in tab #4 [do process D and then] click the button and a new tab (tab #5) opens and Word document D document gets downloaded.
This was caused by using "b.windows.last.use"
So I tried using "b.windows.first.use"
But that fails, because focus isn't going back to tab #1, and watir can't find the object in the modal that it needs to click.
(in `assert_ok': Element is not clickable at point (737.5, -373) (Selenium::WebDriver::Error::UnknownError))
So far as I can tell, I would be fine if I could do any one of the following
get PDFs to download. I cannot. This page seemed promising, but the code didn't work and I couldn't fix it.
get watir to go back to the first page, "for realsies", and find the buttons it needs
perhaps open a new tab for every section of the test (I'm going to look into this one, but I'm not overly hopeful)
Any ideas?
I updated Watir and Ruby both within the last two months. I'm using Chrome on OSX. Moving to Windows or IE are not viable options.
Here is a section of the code. No, I'm not a programmer first and foremost.
Thanks.
currenturl = "stackoverflow.placeholder.com/works"
# print Inventory Report .PDF (document 1)
b.goto currenturl
b.div(:id, 'page').a(:text, 'Inventory Report').click
sleep 1
b.div(:id, 'printInventoryReportModal').a(:text, 'A4').click
b.link(:id => "submitBTNprint-inventory-report").fire_event "onclick"
# print General List .PDF (document 2)
b.goto [main page URL]
b.div(:id, 'page').a(:text, 'General List').click
sleep 1
b.div(:id, 'printGeneralInventoryListModal').a(:text, 'A4').click
b.link(:id => "submitBTNprint-general-list").fire_event "onclick"
# print General List .DOC (document 3)
b.goto [main page URL]
b.div(:id, 'page').a(:text, 'General List').click
sleep 1
b.div(:id, 'printGeneralInventoryListModal').a(:text, '.DOC').click
b.div(:id, 'printGeneralInventoryListModal').a(:text, 'A4').click
b.link(:id => "submitBTNprint-general-list").fire_event "onclick"
It seems mostly that the browser.window function is not working the way you expect, or you are not using it properly. Since your question currently contains very little in terms of examples of how you are trying to change windows (tabs) it's very difficult to tell what you might be doing wrong.
To start with I would use IRB to do some exploring and troubleshooting
Get into IRB, then use
require 'watir-webdriver'
b = Watir::Browser.new :chrome
Now manually navigate the browser window that opens and get it into the state where you have multiple tabs open
Now try something like this in IRB to see if you can get it to spit out window titles
b.windows.each {|window| puts window.title}
If the titles are different, then you should be able to use that to set which window you want to use. If they are not different you will need to look at other attributes to see if there is an easy way to identify which window you want. (or maybe just use :index)
Try below code,
sleep (2)
#browser.driver.switch_to.window(#browser.driver.window_handles[put_your_index])
#browser.window(:title,"Please put your title here").use do
end
#browser.button.when_present.click
when you want to want to switch back to your actual window, use the
same code as above and change the window index accordingly.
For an example, if parent window index is 0 and then child window index would be 1