I am trying to have Python, Chrome and ActionChains to save a webpage.
The code seemed like ever works fine on an computer but not another.
I checked the code and also tried the suggestions gave at selenium action chains no effect, but it still doesn't work.
Where went wrong? thanks.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import win32com.client as comclt
chromedriver = "c:\Python27\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.maximize_window()
aaa = driver.get("https://sg.yahoo.com/?p=us")
time.sleep(3)
save_me = ActionChains(driver).key_down(Keys.CONTROL).key_down('s').key_up(Keys.CONTROL).key_up('s')
save_me.perform()
time.sleep(2)
wsh= comclt.Dispatch("WScript.Shell")
wsh.AppActivate("chrome")
time.sleep(1)
wsh.SendKeys("{ENTER}")
driver.quit()
it seems like the versions for Chrome and Chromedriver (32-bit, 64-bit) matter. Tried another version it may work fine.
Related
i have code it opens website in chrome for this code but it does not go further
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("C:\Drivers\chromedriver_win32 (2)\chromedriver.exe")
driver.get('https://opensource-demo.orangehrmlive.com/web/index.php/auth/login')
driver.find_element("username").send_keys("Admin")
driver.find_element("password").send_keys("admin123")
driver.close()
Result this one
i need answer how to solve this problem
The way you are initializing your webdriver and selecting the input elements seem to be the problem.
Try doing it this way instead.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get('https://opensource-demo.orangehrmlive.com/web/index.php/auth/login')
time.sleep(3)
driver.find_element(By.XPATH, "//input[#name='username']").send_keys("Admin")
driver.find_element(By.XPATH, "//input[#name='password']").send_keys("admin123")
time.sleep(3)
driver.close()
You may also check out selenium's quick start guide here
I'm trying to launch some browser (Chrome) functions by sending shortcuts
I have tried several methods but they all cannot work.
I do this by following these steps
Initialize the browser
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
options = Options()
options.add_argument("--user-data-dir="+r"path_to_user_data")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())
,options=options
)
Through actionchain
ActionChains(driver).key_down(Keys.CONTROL).key_down('T').key_up('T').key_up(Keys.CONTROL).perform
Through send_key
driver.find_element(by=By.XPATH,value="/html/body").send_keys(Keys.CONTROL+"T")
But it doesn't work. This is confused for me. Why it cannot work?
It is possible to open a new tab and switch to it with a single command
driver.switch_to.new_window()
To Open a blank new tab you can use the below:
driver.execute_script("window.open('');")
Switch to the new window
driver.switch_to.window(driver.window_handles[1])
I've been trying to run a script in Python to make Chrome open up to a specific page. Here is my code so far
Code part 1
Code part 2
The code is
from selenium import webdriver as wd
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
Then
driver = wd.Chrome()
driver.implicitly_wait(5)
Then
driver.get("https://www.facebook.com")
Despite the error message in the screenshot after the second cell, Chrome opens when I run the script. It just opens to a blank page. I've tried changing the name of driver and wd and webdriver and I get the "module selenium.webdriver has no attribute "get"" every time. This post from yesterday is similar to what I'm having trouble with
but the solution isn't working for me.
Not sure, but try one of the following:
remove wd as it causing misleading
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver = webdriver.Chrome()
driver.implicitly_wait(5)
or this
from selenium import webdriver as wd
from webdriver_manager.chrome import ChromeDriverManager
driver = wd.Chrome(ChromeDriverManager().install())
driver = wd.Chrome()
driver.implicitly_wait(5)
But please don't mix between them.
I prefer the first option.
I need to open a new browser tab in my test and I've read that the best approach is to simply send the appropriate keys to the browser. I'm using windows so I use ActionChains(driver).send_keys(Keys.CONTROL, "t").perform(), however, this does nothing.
I tried the following to test that Keys.CONTROL is working properly:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def test_trial():
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
ActionChains(driver).send_keys(Keys.CONTROL, "v").perform()
This indeed passes whatever I have copied in the clipboard to the Google search box that is in focus by default.
This is what I want to use but that is not working:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def test_trial():
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
ActionChains(driver).send_keys(Keys.CONTROL, "t").perform()
Nothing seems to happen to the browser, no new tab opened, no dialog box, no notification. Does anyone know why this is?
Try this java Script Executor it should work.
link="https://www.google.com"
driver.execute_script("window.open('{}');".format(link))
Edited code with window handle.
driver=webdriver.Chrome()
driver.get("https://www.google.com")
window_before = driver.window_handles[0]
link="https://www.google.com"
driver.execute_script("window.open('{}');".format(link))
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
driver.find_element_by_name("q").send_keys("test")
try executing this script:
driver.execute_script("window.open('https://www.google.com');")
for example
myURL = 'https://www.google.com'
driver.execute_script("window.open('" + myURL + "');")
You’ve gotten some good answers utilizing JavaScript execution, but I am curious why your example doesn’t work in the first place.
It’s possible that your ActionChains line is executed before the page has fully loaded; you could try adding a wait as follows:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def test_trial():
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(By.TAG_NAME("body")))
ActionChains(driver).send_keys(Keys.CONTROL, "t").perform()
I am doing a simple experiment with Amazon and Webdriver. However, using Webdriver Headless cannot find elements and errors out, but non-headless works.
Any suggestions how to get it working headless?
There is a comment right above the --headless flag.
from selenium import webdriver
import sys
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def get_inventory(url):
chrome_options = Options()
# Making it headless breaks. Commenting
# this line, making it non-headless works.
chrome_options.add_argument("--headless")
chrome_options.add_experimental_option(
"prefs", {'profile.managed_default_content_settings.javascript': 2})
chrome_options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'
driver = webdriver.Chrome(executable_path=os.path.abspath(
'chromedriver'), chrome_options=chrome_options)
driver.set_window_size(1200, 1000)
try:
driver.get(url)
add_to_cart_button_xp = '//*[#id="add-to-cart-button"]'
add_to_cart_button = driver.find_element_by_xpath(add_to_cart_button_xp)
add_to_cart_button.click()
driver.get('https://www.amazon.com/gp/cart/view.html/ref=lh_cart')
qty_field_xp = '//div/input[starts-with(#name, "quantity.") and #type="text"]'
qty_field = driver.find_element_by_xpath(qty_field_xp)
qty_field.clear()
qty_field.send_keys("2")
update_link_xp = f'//input[#value="Update" and #type="submit"]'
update_link = driver.find_element_by_xpath(update_link_xp)
update_link.click()
url = 'https://www.amazon.com/Pexio-Professional-Stainless-Food-Safe-Dishwasher/dp/B07BGBSY9F'
get_inventory(url)
I think you just had some selector issues. I checked the elements and updated the quantity setting; everything else should be pretty much the same, aside from the binary locations.
from selenium import webdriver
import sys
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def get_inventory(url):
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(
executable_path='/usr/bin/chromedriver',
chrome_options=chrome_options,
)
chrome_options.add_experimental_option(
"prefs",
{'profile.managed_default_content_settings.javascript': 2},
)
chrome_options.binary_location = '/usr/bin'
driver.set_window_size(1200, 1000)
try:
driver.get(url)
driver.save_screenshot("/tmp/x1.png")
driver.find_element_by_xpath('//*[#id="add-to-cart-button"]').click()
driver.get('https://www.amazon.com/gp/cart/view.html/ref=lh_cart')
driver.find_element_by_xpath("//span[#data-action='a-dropdown-button']").click()
driver.find_element_by_xpath("//*[#class='a-dropdown-link'][text()[contains(., '2')]]").click()
driver.find_element_by_class_name("nav-logo-base").click()
driver.save_screenshot("/tmp/confirm.png")
driver.close()
except Exception as e:
print(e)
url = 'https://www.amazon.com/Pexio-Professional-Stainless-Food-Safe-Dishwasher/dp/B07BGBSY9F'
get_inventory(url)
I've run this with and without --headless and it's working fine for me. I navigated to the homepage at the end so you can confirm the quantity change worked (hence the screenshot).
What is the behavior you see?
When I enabled headless, scripts started failing because running headless slows execution down.
I currently run chrome with these options:
'--no-sandbox', '--headless', '--window-size=1920,1080', '--proxy-server="direct://"', '--proxy-bypass-list=*'
The last two options supposedly help with the slowness, but I didn't see any difference.
Hope this helps.
I verified your claim on my Mac (using /Applications/Google Chrome.app/Contents/MacOS/Google Chrome).
My guess is that, since you are moving from an item page to the cart page of Amazon, the cookies are lost, so that the cart page won't show any item, and therefore won't contain any text input with a name starting with “quantity”, which is what the exception is about.
Googling for headless chrome cookies yields this page, which in turn points to this page, the content of which could also be about your problem. Be it this, or be it a particularly smart behavior of the Amazon website, the fact remains: the cookie that stores the cart (or a key thereof, but the result is the same) is not read by the cart page when in headless mode.