Automating web based Qlikview using selenium python - python-3.x

I am pretty new to this selenium module in python.
I am trying to automate web based qlikview with selenium and have somewhat managed to do it but facing one problem.
The problem is:
I am able to change tabs and apply filter and export data from the qlickview by typing in the commands in the pycharm console (command prompt) but when i put together the commands and run the code, the code is not executing in the desired manner?
Can somebody help me regarding this?
from selenium import webdriver
driver = webdriver.Chrome()
link = 'some link'
driver.get(link)
driver.implicitly_wait(15)
click_1 = driver.find_element_by_xpath("path").click()
driver.implicitly_wait(2)
click_2 = driver.find_element_by_xpath('path').click()
It will be very appreciated. Please suggest.

Related

Selenium Python - Internet Explorer - Problem

I'm new on Selenium and I'm trying to do a task using Internet Explorer (I'm using Python 3.8)
In order to uderstand the commands of Selenium, I tried to run the simple code below
from selenium.webdriver.common.keys import Keys
import time
driver=webdriver.Ie(executable_path="C:\\Users\\usuario\\Downloads\\IEDriverServer.exe")
driver.get("https://www.google.com.br/")
driver.set_page_load_timeout(10)
driver.find_element_by_name("q").send_keys("Ronaldinho Gaucho")
driver.find_element_by_name("btnK").send_keys(Keys.ENTER)
driver.maximize_window()
driver.refresh()
The page opens, however, nothing is typed on the search bar on Google website, I have seen this code in a Youtube video and it has worked well, but when I try on my computer, it does not works (it does not raise any error on my terminal)
Anyone can help?, what I should been looking for?
Thanks in advance

Finding a link Element in Selenium which contains a specific word in its href with python

I am windows 7 user using python 3.6.7 and chromedriver 83.3
I love automating stuff with python and recently started web automation with selenium and chromedriver. So i am pretty new to this field.
I wrote a script that can download any software from the internet after, (spending hours on tutorials and documentation reading) upon giving it a search query. Here is my script:
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
import requests, bs4
query = input("Name for a windows software: ")
searchGoogle = "https://www.google.com/search?q="+"download "+str(query)+" for windows 7"
driver = webdriver.Chrome('chromedriver.exe')
links = []
website = requests.get(searchGoogle)
website_text = website.text
soup = bs4.BeautifulSoup(website_text,"lxml")
all_links = []
for link in soup.find_all("a"):
links.append(link.get("href"))
for link in links:
if "/url?q=" in link:
final = link.replace("/url?q=","")
final = final.split("&", 1)[0]
all_links.append(final)
for ss in all_links:
try:
driver.get(ss)
time.sleep(30)
download = driver.find_element_by_partial_link_text('Download')
download.click()
print(download.text)
quit()
except:
#print(download.href)
print("Not Found... Moving to next...")
continue
the problem is that sometimes it clicks on some links that say "Download" and go to another page which asks "Start Download".
I know that when you download an exe file the link to download contains something like this: "https://something.com/something/something.exe"
So i wanted to ask if there was a find_element_if_its_href_contains('.exe')
Or: anything that clicks only a link which contains ".exe" in it.
I am new to this community and I am sorry if you find anything in my question that does not meet StackOverflow Expectations. Ask me in the comments and i would be glad to change my question in the way that you suggest.
By The Way, Thanks in advance!
You can create an xpath or css expression to match webelements with href containing the string ".exe":
driver.find_element_by_xpath("//*[contains(#href,'.exe')]")
#or
driver.find_element_by_css_selector("[href*='.exe']")
I use selenium ver.3.141.0 and I also got another way to fix this problem.
I just wanted to give some 'partial' string and get the 'whole' link text. The code is below:
all_links_by_keyword = driver.find_elements_by_partial_link_text(your_variables + 'any_string_you_need')
For instance, if you want to find links with 'computer' but the page has lots of links including 'computer', like 'computerV01', 'computerV02'...etc.
Then you can use this to find all links with 'computer' keyword.
Example code:
getComputer = 'computer'
links = driver.find_elements_by_partial_link_text(getComputer + 'V')
for link in links:
print(link.get_attribute('href'))
Hope it can help you.

Python Selenium is not opening the link

Python Selenium is opening a tab but not the link as shown in the given image
The issue is that when I run it in Linux, it does not open the link as shown in this image
terminal:- here you go
Thanks for your time
Can you please try below solution :
from selenium import webdriver
while True:
driver = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver")
driver.get("http://google.com/")
driver.quit()

how to select the first entry in jira using python selenium

I'm new to python. I want to select the first entry of my query list in Jira using python selenium.
import selenium
chrome_path = "C:\Python37-32\chromedriver.exe"
from selenium import webdriver
driver = webdriver.Chrome(chrome_path)
driver.get('https://jira.xxxx.com/issues/?filter=24005')
driver.find_element_by_xpath('//*[#id="user-options"]/a').click()
driver.find_element_by_xpath('//*[#id="login-form-username"]').send_keys('xxxx')
driver.find_element_by_xpath('//*[#id="login-form-password"]').send_keys('xxxx')
driver.find_element_by_xpath('//*[#id="login-form-submit"]').click()
driver.find_element_by_xpath('//*#id="content"]/div[1]/div[3]/div/div/div/div/div/div/div[1]/div[1]/span/span[3]').text
need to select every entry in the query list.
Kindly help
Thanks in advance
If you're able I would recommend using the Jira API for something like this (are you using cloud)? https://jira.readthedocs.io/en/master/installation.html
You would use your filter https://domain.atlassian.net/issues/?filter=24005 and you would have gotten all issues in this API request.
Example:
from jira import JIRA
issue_list = jira.search_issues("filter=24005")
for issue in issue_list:
DO SOMETHING

Python Selenium : How to hide geckodriver?

I am writing an program for a web automation in python. Is here a ways to hide the geckodriver? So that the console (see picture) won't show up when I start the program.
console of geckodriver
here is a fraction of my code:
from selenium import webdriver
from selenium import *
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC`
driver=webdriver.Firefox()
wait=WebDriverWait(driver,120)
url = r"http://google.com"
driver.get(url) #This line starts the console (see picture)
To prevent geckodriver from displaying any windows, you need to pass -headless argument:
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.add_argument('-headless')
driver = webdriver.Firefox(options=options)
driver.get('https://www.example.com')
...
driver.quit()
This worked for me in C#. It blocks both geckodriver and firefox window
FirefoxOptions f = new FirefoxOptions();
f.AddArgument("-headless");
var ffds = FirefoxDriverService.CreateDefaultService();
ffds.HideCommandPromptWindow = true;
driver = new FirefoxDriver(ffds,f);
I was able to do that after implementing PyVirtualDisplay
sudo pip install pyvirtualdisplay # Install it into your Virtual Environment
Then just import Display as follows:
from pyvirtualdisplay import Display
Then, before fetching, start the virtual display as follows:
# initiate virtual display with 'visible=0' activated
# this way you will hide the browser
display = Display(visible=0, size=(800, 600))
# Start Display
display.start()
...
# Do your fetching/scrapping
...
# Stop Display
display.stop()
I hope it helps
Here the approach which has solved it in my case. I used #thekingofravens suggestion but realized that it is just enough to create the Run.bat file which he mentions in his post. Previously I ran my programms in IDLE with F5. Because of this the Geckodriver popped up every few seconds.
My Solution: I just made the Run.bat file with the same code:
cd C:\PathToYourFileWhichIsCausingTheGeckodriverToPopUp
python FileWhichIsCausingTheGeckodriverToPopUp.py
And thats it. Just start this file when you want to run your code and the Geckodriver won't pop up. (That it works without the whole path to you program, Python has to be in PATH.)
Also, of course you can run your program from the command line with the same commands like above and without creating an extra bat file.
You have to take care of a couple of stuffs here:
Keep the useful imports. Unusual imports like from selenium import * (in your code) must be avoided.
Keep your code minimal. wait=WebDriverWait(driver,120) have no usage in your code block.
When you use the raw r switch remember to use single quotes '...'
If you initialize the webdriver instance remember to call the quit() method.
Be careful about indentation while using Python.
Here is the minimal code which uses geckodriver to open the url http://www.google.com, print the Page Title and quits the driver instance.
from selenium import webdriver
driver=webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
url = r'http://www.google.com'
driver.get(url)
print("Page Title is : %s" %driver.title)
driver.quit()
Console Output:
Page Title is : Google
So I found a very generic workaround to solve this on Windows (in Linux it doesn't seem to be an issue in the first place, can't speak for OSX).
So you need to make three files and its very awkward.
First, you make a file. call it start.bat (or anything) and put the following code in it:
wscript.exe "C:\Wherever\invisible.vbs" "C:\Some Other Place\Run.bat"
This will be the top level batch script. It will be visible for a split second while it launches a visual basic script and passes a batch script as an argument. The purpose of this is to make the next console invisible. Next we make the VBscript, invisible.vbs:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
Finally, we make the script that invisible.vbs is supposed to hide, we could call it Run.bat
cd C:\Wherever
python script_using_geckodriver.py
What happens is, as follows:
The first .bat file launches invisible.vbs
invisible.vbs launches the second .bat file without showing it on screen.
The second .bat file then launches the python program. Python (and geckodriver) output to the invisible cmd therefore hiding the geckodriver console window.
P.S. all of this works with PyInstaller to produce a single redistributable package the user can just click on.
Credit harrymc # superuser.com for this solution, which I found when trying to solve an unrelated problem. I tested and realized it was cross applicable to this.
https://superuser.com/questions/62525/run-a-batch-file-in-a-completely-hidden-way

Resources