Multikey press not working in python selenium - python

I am trying to press Ctrl+s and then click enter on a webpage to save its html. But the multikey pressing functionality is not working out. I have tried way1 and way2 but both didn't work. However, If I do action.send_keys('s') by without executing action.key_down(Keys.CONTROL) before it, it works fine. This is my full code:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(executable_path="chromedriver.exe")
# get method to launch the URL
driver.get("https://www.google.ca/")
time.sleep(3)
action = ActionChains(driver)
# click google search bar (to make sure driver is working)
driver.find_element_by_name("q").click()
print("passed1")
time.sleep(3)
# way 1
action.key_down(Keys.CONTROL).send_keys('s').key_up(Keys.CONTROL).perform()
print("passed2")
# way 2
action.key_down(Keys.CONTROL)
action.send_keys('s')
action.key_up(Keys.CONTROL)
action.perform()
print("passed2")
time.sleep(100)
driver.close()
Can someone please explain to me what is he issue? I've been trying to figure it out for an hour now.

After looking around, I found that it is not advisable to interact with the browser for this purpose. It is better to get the page_source instead.
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r"C:\Program Files (x86)\Selenium\chromedriver.exe")
driver.get("http://www.example.com")
with open('page.html', 'w', encoding='utf-8') as f:
f.write(driver.page_source)

Related

python selenium having difficulties with click

Currently using python and trying to have selenium click the "About" on google without using id. When I try to use .click() it does not execute, what is wrong with my code? I have looked at many videos and tutorials and it looks correct.
from selenium import webdriver
from time import sleep
browser = webdriver.Safari()
browser.get('http://google.com')
browser.maximize_window()
elm = browser.find_element_by_link_text('About')
browser.implicitly_wait(5)
elm.click()
I think, you can try using find_element_by_xpath.
First you will copy xpath of about link then you can try like below:
from selenium import webdriver
from time import sleep
browser = webdriver.Safari()
browser.get('http://google.com')
browser.maximize_window()
elm = browser.find_element_by_xpath('//*[#id="fsl"]/a[3]')
browser.implicitly_wait(5)
elm.click()
So the issue ended up being safari. For some reason the web driver safari was not allowing me to use .click. I switched to chrome and it worked.

Why doesn't selenium click?(Python)

The other day, I started using the Selenium library to press buttons on YouTube, and everything was fine, but the other day, for some reason, it stopped pressing buttons, I didn’t change anything, everything was exactly the same, and when I checked the code on another computer, everything was fine, and most importantly, he didn’t even
gives me errors and pretends that everything went well!
What to do in this situation, I will be glad to every answer! (Worked in python)
from selenium import webdriver
import time
url = 'url video'
FILE_NAME_PROFILE = "C:/Users/xxx/AppData/Local/Google/Chrome/User Data"
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=" + FILE_NAME_PROFILE)
driver =webdriver.Chrome(executable_path="C:/Users/xxx/Downloads/chromedriver_win32(1)/chromedriver.exe",chrome_options=options)
driver.get(url)
time.sleep(10)
like = driver.find_element_by_xpath('//yt-icon[#class="style-scope ytd-toggle-button-renderer"]')
like.click()
driver.quit()
Try adjusting as thus:
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait ##new line of code
url = 'url video'
FILE_NAME_PROFILE = "C:/Users/xxx/AppData/Local/Google/Chrome/User Data"
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=" + FILE_NAME_PROFILE)
driver =webdriver.Chrome(executable_path="C:/Users/xxx/Downloads/chromedriver_win32(1)/chromedriver.exe",chrome_options=options)
driver.get(url)
time.sleep(10)
inp_xpath_search = '//yt-icon[#class="style-scope ytd-toggle-button-renderer"]'
like = WebDriverWait(driver,50).until(lambda driver: driver.find_element_by_xpath(inp_xpath_search))
like.click()
time.sleep(2)
driver.quit()

Selenium not storing cookies

I'm working on trying to automate a game I want to get ahead in called pokemon vortex and when I login using selenium it works just fine, however when I attempt to load a page that requires a user to be logged in I am sent right back to the login page (I have tried it outside of selenium with the same browser, chrome).
This is what I have
import time
from selenium import webdriver
from random import randint
driver = webdriver.Chrome(r'C:\Program Files (x86)\SeleniumDrivers\chromedriver.exe')
driver.get('https://zeta.pokemon-vortex.com/dashboard/');
time.sleep(5) # Let the user actually see something!
usernameLoc = driver.find_element_by_id('myusername')
passwordLoc = driver.find_element_by_id('mypassword')
usernameLoc.send_keys('mypassword')
passwordLoc.send_keys('12345')
submitButton = driver.find_element_by_id('submit')
submitButton.submit()
time.sleep(3)
driver.get('https://zeta.pokemon-vortex.com/map/10')
time.sleep(10)
I'm using python 3.6+ and I literally just installed selenium today so it's up to date, how do I force selenium to hold onto cookies?
Using a pre-defined user profile might solve your problem. This way your cache will be saved and will not be deleted.
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data")
driver = webdriver.Chrome(options=options)
driver.get("xyz.com")

Send Ctrl+f python selenium chrome

I am able to use some keys like Tab
browser.find_element_by_tag_name('body').send_keys(Keys.TAB)
But not Ctrl+f or Ctrl+p
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL +'f')
I also tried using xpath find_element_by_xpath & send_keys(Keys.CONTROL,'f') but still not working
You shall try by using action_chains in selenium webdriver
from selenium.webdriver.common.action_chains import ActionChains
#
# Your code
#
browser.find_element_by_tag_name('body')
ActionChains(browser).send_keys(Keys.CONTROL, "f").perform()
To be honest I have not known the answer before. I searched and run code using selenium.webdriver.common.action_chains but failed.
Then after more research, I was suggested to use pyautogui library. I don’t know how, but it worked for me… you can try the code.
# Import necessary libraries
from selenium import webdriver
import pyautogui
# to get the broswer
driver = webdriver.Chrome(executable_path="C:\Windows\chromedriver_win32 (1)\chromedriver.exe")
driver.maximize_window()
driver.get("https://www.google.com/")
# code to perform ctrl+F
pyautogui.hotkey('ctrl', 'f')

How to refresh an already opened web page

I just want to refresh an already opened web page with Selenium.
It always opens a new browser window.
What I'm doing wrong?
from selenium import webdriver
import urllib
import urllib2
driver = webdriver.Firefox()
driver.refresh()
I would suggest binding the driver element search to the tag body and use the refresh command of the browser.
In OSX for example
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'r')
Documentation on keys here: http://selenium-python.readthedocs.org/en/latest/api.html
Update:
The following code, very similar to your one, works fine for me.
driver = webdriver.Firefox()
driver.get(response.url) #tested in combination with scrapy
time.sleep(3)
driver.refresh()
Are you sure you correctly load the web page with the driver before refreshing it ?
The problem is you are opening the webdriver and then trying to refresh when you have not specified a URL.
All you need to do is get your desired URL before refreshing:
from selenium import webdriver
import urllib
import urllib2
driver = webdriver.Firefox()
driver.get("Your desired URL goes here...")
#now you can refresh the page!
driver.refresh()
The following codes work for me
driver.get(driver.current_url)
sleep(2)
driver.refresh()
I use python 3.7.6, selenium 3.141.0
You are trying to refresh the page before it loads so u can use a sleep function
from time import sleep
sleep(1)
or you can wait for an XPath to load so
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, xpath goes here)))
For me helped
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("URL")
time.sleep(5)
driver.refresh()
I got mine fixed by adding "browser.refresh()" the for loop or while loop.
You can try any one of the below methods for the same.
Method 1:
driver.findElement(By.name("s")).sendKeys(Keys.F5);
Method 2:
driver.get(driver.getCurrentUrl());
Method3:
driver.navigate().to(driver.getCurrentUrl());
Method4:
driver.findElement(By.name("s")).sendKeys("\uE035");

Categories

Resources