I am using chromedriver.exe for Chrome version 80, and chrome version 80.0.3987.149. My selenium is up to date. I'm trying to send shortcuts to the browser in python and nothing is working. I've tried ActionChains, and elem.send_keys(). No shortcuts I've used have worked and I can't figure this out.
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
import pyautogui
driver = webdriver.Chrome(executable_path=r'path/to/exe')
driver.get('https://www.google.com')
time.sleep(8)
elem = driver.find_element_by_xpath("//body")
elem.send_keys(Keys.CONTROL,'t') #Opens a new tab
This doesn't work! I've tried an ActionChain approach and that doesn't work either. Any help would be awesome.
Related
I am trying to get in this website: "https://core.cro.ie/".
I can get in using normal web search, but I can't get in using selenium.
My code looks like this:
site= "https://core.cro.ie/"
driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()))
driver.get(site)
driver.maximize_window()
Any ideas? Thank you very much
This code works fine for navigation ( I dont have Edge browser):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
site= "https://core.cro.ie/"
driver = webdriver.Firefox()
driver.get(site)
driver.maximize_window()
I have installed selenium prior running the test. It seems like the website has some sort of bot prevention mechanism but navigation works fine:
pip install selenium
I am trying to send a ALT+ESC command to my selenium chrome-driver to send it to the back of all other windows
This is the relevant code
from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.send_keys(Keys.LEFT_ALT, Keys.ESCAPE)
actions.perform()
this is not working please help
To press key-combo:
from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).key_down(Keys.LEFT_ALT).send_keys(Keys.ESCAPE).key_up(Keys.LEFT_ALT).perform()
But seems this is an OS-level keys combination for work with windows and this will not work in the selenium context.
Selenium actions applied to web page elements, fire some events inside browser.
ultimately I couldn't find a way to execute a OS command in selenium.
The following code has the same functionality but dose not necessarily run on the browser
from pyautogui import hotkey
hotkey('altleft', 'esc')
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)
I’m working to make web crawler with python by using selenium
Here, I successfully got contents by using chromedriver, but problem occurred when I tried to make
Headless access crawling through PhantomJS. find_element_by_id, or find_element_by_name did not work
Is there any difference between these? Actually I am trying to make this as headless because I want to run this
Code in ubuntu server as a batch job without GUI support.
My script is like as below.
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import re
#driver = webdriver.PhantomJS('/Users/user/Downloads/phantomjs-2.1.1-macosx/bin/phantomjs')
#driver = webdriver.Chrome('/Users/user/Downloads/chromedriver')
driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550)
driver.get(url)
driver.implicitly_wait(3)
#here I tried two different find_tag things but both didn’t work
user = driver.find_element(by=By.NAME,value="user:email")
password = driver.find_element_by_id('user_password')
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')