ActionChains 不使用 Firefox 执行 F5 按下
ActionChains not performing F5 pressing using Firefox
这困扰了我很长时间,如果有人能帮我找出程序中的错误,我们将不胜感激。
谢谢。
class amz_bot():
def __init__(self):
self.driver = webdriver.Firefox()
def login(self):
self.driver.get("http://www.amazon.com/")
time.sleep(5)
while True:
ActionChains(self.driver).send_keys(Keys.F5).perform()
time.sleep(5)
bot = amz_bot()
bot.login()
F5 好像哪儿也去不了。
Selenium 启动浏览器,但 ActionChain
对象是低级按键。即使它是使用驱动程序对象创建的,它也没有 window.
的上下文
如果您要发送任何其他(正常)密钥 - 它们发送到哪里?
一个解决方案是使用 send_keys_to_element
而不是盲目 send_keys
。
这会将焦点放在 window 然后发送 f5.
在我的解决方案中,我假设它只有一个 html
标签...这是一个合理的假设,但是,我的第一次尝试使用了 body
然后结果是3 pf 那些。
所以...试试这个:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
class amz_bot():
def __init__(self):
self.driver = webdriver.Chrome()
def login(self):
self.driver.get("http://www.amazon.com/")
time.sleep(5)
while True:
e = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, 'html')))
ActionChains(self.driver).send_keys_to_element(e, Keys.F5).perform()
time.sleep(10)
bot = amz_bot()
bot.login()
这对我有用 - 但它很奇怪。当 selenium 按 F5(所以它正在按下按键!)页面没有刷新,而是进入搜索结果。
稍后我会再看一遍(我会更新答案),但这是朝着正确方向迈出的一步。至少按键会发生一些事情。
更新 - 另一种选择是使用 javascript。这不是 F5,而是刷新页面:
self.driver.execute_script("location.reload()")
值得一试,因为它很简单,但它可能类似于标准 browser.refresh()
另一个更新:
另一种方法是将浏览器导航到当前 url - 执行与刷新相同的工作
self.driver.get(self.driver.current_url)
这困扰了我很长时间,如果有人能帮我找出程序中的错误,我们将不胜感激。
谢谢。
class amz_bot():
def __init__(self):
self.driver = webdriver.Firefox()
def login(self):
self.driver.get("http://www.amazon.com/")
time.sleep(5)
while True:
ActionChains(self.driver).send_keys(Keys.F5).perform()
time.sleep(5)
bot = amz_bot()
bot.login()
F5 好像哪儿也去不了。
Selenium 启动浏览器,但 ActionChain
对象是低级按键。即使它是使用驱动程序对象创建的,它也没有 window.
如果您要发送任何其他(正常)密钥 - 它们发送到哪里?
一个解决方案是使用 send_keys_to_element
而不是盲目 send_keys
。
这会将焦点放在 window 然后发送 f5.
在我的解决方案中,我假设它只有一个 html
标签...这是一个合理的假设,但是,我的第一次尝试使用了 body
然后结果是3 pf 那些。
所以...试试这个:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
class amz_bot():
def __init__(self):
self.driver = webdriver.Chrome()
def login(self):
self.driver.get("http://www.amazon.com/")
time.sleep(5)
while True:
e = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, 'html')))
ActionChains(self.driver).send_keys_to_element(e, Keys.F5).perform()
time.sleep(10)
bot = amz_bot()
bot.login()
这对我有用 - 但它很奇怪。当 selenium 按 F5(所以它正在按下按键!)页面没有刷新,而是进入搜索结果。
稍后我会再看一遍(我会更新答案),但这是朝着正确方向迈出的一步。至少按键会发生一些事情。
更新 - 另一种选择是使用 javascript。这不是 F5,而是刷新页面:
self.driver.execute_script("location.reload()")
值得一试,因为它很简单,但它可能类似于标准 browser.refresh()
另一个更新:
另一种方法是将浏览器导航到当前 url - 执行与刷新相同的工作
self.driver.get(self.driver.current_url)