Using Selenium to select a menu item from a ribbon - python-3.x

I am attempting to use Selenium to automate tasks in the office. One such task involves jumping onto a website and navigating its menus to a specific page. In this case, I am trying to have get selenium to select the Applications menu, then the Warranty Tools sub menu, then the Warranty Navigator link.
Below is the code I worked out so far. It will load into the website correctly and navigate to the opening page. From there I am attempting to get it to select the 'Applications' menu. Instead, the program times out without selecting the intended element. I have attempted to use css_selector, xpath(included in example), and class to no avail.
import selenium
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import Select
from time import sleep
driver = webdriver.Chrome()
driver.get('https://www.solutionnavigator.com/s/login/?language=en_US')
driver.maximize_window()
email = WebDriverWait(driver,10).until(EC.element_to_be_clickable(('xpath','element').send_keys('email')
password = WebDriverWait(driver,10).until(EC.element_to_be_clickable(('xpath','element'))).send_keys('password')
login = WebDriverWait(driver,10).until(EC.element_to_be_clickable(('xpath','element'))).click()
menu_content = driver.find_element('xpath,'element').click()
time.sleep(20)

Related

Accepting cookie pop-up with Selenium in Python

I need to scrape a website, but first I need to remove the pop-up window for cookies. Following other questions, I have written the following code (with the appropriate link for url):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
PATH="C:\\Program Files (x86)\\chromedriver.exe"
driver=webdriver.Chrome(PATH)
driver.maximize_window()
url='https://dait.interno.gov.it/elezioni/open-data'
driver.get(url)
sel='#popup-buttons > button.agree-button.eu-cookie-compliance-default-button'
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,sel))).click()
What confuses me is that it seemed to have worked once, and only once even if I didn't change anything in the code. Now it doesn't do anything anymore and I get:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point
I have obtained the CSS locator by "copying selector" when inspecting the page.

Cannot scroll all the way down the page, because it keeps refreshing Selenium Python

I am trying to automate saving website's description and url. I loop the program and come to the function get_info(). Basically it need to add the first website on the google page I load and scroll down so when it executes again it can add other websites. The problem is that the program refresh the page everytime it executes get_info() and brings you back at the top.
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time
browser=webdriver.Firefox()
def get_info():
browser.switch_to.window(browser.window_handles[2])
description = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "h3"))
).text
site = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.TAG_NAME, "cite"))
)
site.click()
url=browser.current_url
browser.back()
browser.execute_script("window.scrollBy(0,400)","")
To stop Chrome from auto-reloading , you can do this
driver.execute_cdp_cmd('Emulation.setScriptExecutionDisabled', {'value': True})
Link to read about this Chrome Devtools flag - here
The question already has been asked here

How to zoom in a webpage using Python Selenium in FireFox

I am working on Ubuntu and writing a web automation script which can open a page and zoom in the page. I am using python selenium. Below is the code:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://www.wikipedia.org/")
driver.maximize_window()
time.sleep(2)
This opens the browser and opens the wikipedia page. Now I have to do the zoom in functionality. For this I tried below line of code:
driver.execute_script("document.body.style.zoom='150%'")
It didn't work. So I tried below line of code:
driver.execute_script('document.body.style.MozTransform = "scale(1.50)";')
driver.execute_script('document.body.style.MozTransformOrigin = "0 0";')
It kind of worked but the webpage height and width were disturbed and it looks like below:
It should look like below:
Can anyone please suggest some good working solution to zoom in a webpage using python selenium in FireFox.
I didn't find any working solution so what I did is, I used pyautogui to simulate the keys.
pyautogui.keyDown('ctrl')
pyautogui.press('+')
pyautogui.keyUp('ctrl')
This made the webpage zoom to 110%. You can run it again to make it to 120% and so on.
Thanks

python selenium doesnot bypass firefox download dialog even though preferences have been set

I am trying to download file (market Summary (Closing) — ZIP ) from the following link:
https://dps.psx.com.pk/downloads
Here is my code:
from datetime import date
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.preferences.instantApply",True)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
profile.set_preference("browser.helperApps.alwaysAsk.force",False)
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.download.folderList",0)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://dps.psx.com.pk/downloads')
start_date = driver.find_element_by_id('downloadsDatePicker')
start_date.send_keys(Keys.BACKSPACE*10)
send_date = date(2014,1,1).strftime("%Y-%m-%d")
start_date.send_keys(send_date)
driver.find_element_by_id('downloadsSearchBtn').click()
link = '/download/mkt_summary/{}.Z'.format(send_date)
time.sleep(5)
driver.find_element_by_css_selector("a[href='{}'".format(link)).click()
Even though I have set the preferences, firefox still pops up a download dialog box. Why is it so and how can I avoid this behavior?
Based on the site and link you are preparing, you are not downloading csv file but mostly a zip file with .Z extension.
You need to change the code for preference browser.helperApps.neverAsk.saveToDisk to allow the response received from the site as shown below.
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream")

Web scraping of a betting site with selenium. Incomplete event list

I wrote a program to get some odds from the "Eurobet.it" site using selenium to open the page containing the "ChanceMix". It seems that it works, but when the games take place in more than one date the list I get is incomplete. It contains only those of the first/second date, but not the next ones. I tried various solutions but without results. Can someone help me?
This is my code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
link = 'https://www.eurobet.it/it/scommesse/#!/calcio/eu-champions-league/'
driver = webdriver.Chrome()
driver.get(link)
# Find the "ChanceMix" button by xpath and store it as a webdriver object
element = driver.find_element_by_xpath("//a[contains(text(), 'ChanceMix')]")
# Click on the "ChanceMix" button to open the relevant page
element.click()
# Find the list of games
Games = ui.WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.box-sport")))
# Print the list of games
for x in range(0,len(Games)): print(Games[x].text)

Resources