Selenium Python - Internet Explorer - Problem - python-3.x

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

Related

Selenium webdriver python element screenshot not working properly

I looked up Selenium python documentation and it allows one to take screenshots of an element. I tried the following code and it worked for small pages (around 3-4 actual A4 pages when you print them):
from selenium.webdriver import FirefoxOptions
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("browser.privatebrowsing.autostart", True)
# Configure options for Firefox webdriver
options = FirefoxOptions()
options.add_argument('--headless')
# Initialise Firefox webdriver
driver = webdriver.Firefox(firefox_profile=firefox_profile, options=options)
driver.maximize_window()
driver.get(url)
driver.find_element_by_tag_name("body").screenshot("career.png")
driver.close()
When I try it with url="https://waitbutwhy.com/2020/03/my-morning.html", it gives the screenshot of the entire page, as expected. But when I try it with url="https://waitbutwhy.com/2018/04/picking-career.html", almost half of the page is not rendered in the screenshot (the image is too large to upload here), even though the "body" tag does extend all the way down in the original HTML.
I have tried using both implicit and explicit waits (set to 10s, which is more than enough for a browser to load all contents, comments and discussion section included), but that has not improved the screenshot capability. Just to be sure that selenium was in fact loading the web page properly, I tried loading without the headless flag, and once the webpage was completely loaded, I ran driver.find_element_by_tag_name("body").screenshot("career.png"). The screenshot was again half-blank.
It seems that there might be some memory constraints put on the screenshot method (although I couldn't find any), or the logic behind the screenshot method itself is flawed. I can't figure it out though. I simply want to take the screenshot of the entire "body" element (preferably in a headless environment).
You may try this code, just that you need to install a package from command prompt using the command pip install Selenium-Screenshot
import time
from selenium import webdriver
from Screenshot import Screenshot_Clipping
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://waitbutwhy.com/2020/03/my-morning.html")
obj=Screenshot_Clipping.Screenshot()
img_loc=obj.full_Screenshot(driver, save_path=r'.', image_name='capture.png')
print(img_loc)
time.sleep(5)
driver.close()
Outcome/Result comes out to be like, you just need to zoom the screenshot saved
Hope this works for you!

Automating web based Qlikview using selenium python

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.

get active url on chrome and firefox using python

I work with python 3.7 on windows and I want to write a script that print the actual active urls on browsers (chrome and firefox), I found the script:
import webbrowser
webbrowser.open(the url)
but this script allows to open the url not to find the active urls.
can someone help me
Here you need to download and install pywin32 and import these modules in your script like this -
import win32gui
import win32con
#to get currently active windows
window = win32gui.GetForegroundWindow
Or to get the Google Chrome window handle
win32gui.FindWindow
You can use selenium module and loop through all open tabs.
This code prints all open tabs of chrome and firefox:
from selenium import webdriver
chromeDriver = webdriver.Chrome()
firefoxDriver = webdriver.Firefox()
for handle in chromeDriver.window_handles[0]:
chromeDriver.switch_to.window(handle)
print(chromeDriver.current_url)
for handle in firefoxDriver.window_handles[0]:
firefoxDriver.switch_to.window(handle)
print(firefoxDriver.current_url)
Note:
This code is inefficient and should change to use only one loop.

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()

Adapting Selenium Script for Raspberry Pi

Preface: I have very limited experience with linux and selenium in general. Also this is a multipart question.
Hi there,
I've written a script that I want to run on my Raspberry Pi model 3B which is running Raspbian OS.
I originally built it on a Windows machine (pretty stupid mistake to build it for the wrong platform but anyway) and now need to adapt it so it runs on the Pi.
The main thing I am concerned about is the usage of selenium and chromedriver. I have downloaded chromium-chromedriver (chromium-chromedriver 34.0.1847.116-0ubuntu2 in armhf (Release)) from this repository.
First Question
The file downloads as a .deb which I have never seen before. It opts to install, however I have no idea where it installs as it does not give the option to install to a different directory. I've tried search for it with find, but nothing comes up. Where does it go by default in Raspbian OS?
Second Question
I know I will have to change some expressions because I am using chromium-chromedriver instead of chrome driver, but I am unsure which ones as I don't know selenium that well. This is the main instance here. Do I just change all instances of "Chrome" including "chrome_options" to "Chromium/chromium_options"?
from selenium.webdriver import Chrome
from contextlib import closing
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
#Scrape out the table information
with closing(Chrome(chrome_options=chrome_options)) as driver:
Lastly, for the purpose of finding the process with psutil and ending it with process.kill(), the text ID of the driver in task manager should just be chromium-chromedriver.exe right?
Any and all help and advice appreciated.
Thank you all for your time.
L

Resources