gpt4 book ai didi

python - Keys.BACK_SPACE 或 Keys.TAB 发送 key 不适用于 facebook selenium python

转载 作者:行者123 更新时间:2023-12-02 16:12:56 28 4
gpt4 key购买 nike

我正在编写一些 selenium 代码来导航 Facebook。

import os
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys


def driver():
global driver
driver = webdriver.Chrome("chromedriver.exe", chrome_options=chrome_options)
driver.get("https://facebook.com")


def chrome_options():
global chrome_options
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('--profile-directory=Default')
# chrome_options.add_argument("--user-data-dir=chrome-data")
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument('disable-infobars')
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])


def actions():
global actions
actions = ActionChains(driver)


def login():
try:
# I use environment veriable base on this tutorials https://www.youtube.com/watch?v=IolxqkL7cD8
username = os.environ.get('my_facebook_username')
password = os.environ.get('my_facebook_password')

driver.find_element_by_name("email").send_keys(username)
driver.find_element_by_name("pass").send_keys(password)
driver.find_element_by_name("login").click()
print(input("Press any Key: "))
print("Login work Successfully ")

except:
pass


def navigatePagePostAria():
sleepTime = 4
implicitlyWaitTime = 20
for i in range(2):
driver.implicitly_wait(implicitlyWaitTime)
actions.send_keys(Keys.BACK_SPACE)
actions.send_keys(Keys.TAB * 4)
time.sleep(sleepTime)
actions.perform()
print("Firast 10 tabs Working")

actions.send_keys(Keys.TAB * 3)
actions.send_keys(Keys.ENTER)
actions.perform()
print("Navigate Post area Successfully ")


chrome_options()
driver()
login()
driver.get("https://www.facebook.com/groups/402353916617590/permalink/1630582000461436/")
navigatePagePostAria()

错误来自最后一个函数 navigatePagePostAria()该行将是actions.send_keys(Keys.BACK_SPACE)

错误:

Traceback (most recent call last):
File "K:\Project\Python\Miracle\groupPost.py", line 154, in <module>
navigatePagePostAria()
File "K:\Project\Python\Miracle\groupPost.py", line 63, in navigatePagePostAria
actions.send_keys(Keys.BACK_SPACE)
AttributeError: 'function' object has no attribute 'send_keys'

这两行对于导航来说是最重要的

1.actions.send_keys(Keys.BACK_SPACE)

2.actions.send_keys(Keys.TAB * 4)

我用它代替 x 路径,因为 facebook 非常动态,它每隔 minit 就会改变它的 x 路径。

我的问题的视频描述: https://youtu.be/BzSBLAaYS-s

最佳答案

这里的问题是您在方法中使用了具有误导性的名称。
您定义了以下方法

def actions():
global actions
actions = ActionChains(driver)

所以现在当你调用 actions.send_keys(Keys.BACK_SPACE) 时,解释器认为你正在调用 actions 方法,而不是 actions 对象在其中启动。
我建议您将此方法定义如下

def set_actions():
global actions
actions = ActionChains(driver)

或者最好根本不定义此方法,因为这不是您要多次调用的真正方法。
chrome_optionsdriver 方法也是如此。
没有必要将它们定义为方法,因为它们不是可重用的方法,它最好是扁平代码,这样整个代码将如下所示:

import os
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

#Setting the chrome_options
global chrome_options
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument('--profile-directory=Default')
# chrome_options.add_argument("--user-data-dir=chrome-data")
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument('disable-infobars')
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])

#Setting the Chrome Driver
global driver
driver = webdriver.Chrome("chromedriver.exe", chrome_options=chrome_options)

#Setting the Actions
global actions
actions = ActionChains(driver)


def login():
driver.get("https://facebook.com")
try:
# I use environment variable base on this tutorials https://www.youtube.com/watch?v=IolxqkL7cD8
username = os.environ.get('my_facebook_username')
password = os.environ.get('my_facebook_password')

driver.find_element_by_name("email").send_keys(username)
driver.find_element_by_name("pass").send_keys(password)
driver.find_element_by_name("login").click()
print(input("Press any Key: "))
print("Login work Successfully ")

except:
pass


def navigatePagePostAria():
sleepTime = 4
implicitlyWaitTime = 20
for i in range(2):
driver.implicitly_wait(implicitlyWaitTime)
actions.send_keys(Keys.BACK_SPACE)
actions.send_keys(Keys.TAB * 4)
time.sleep(sleepTime)
actions.perform()
print("Firast 10 tabs Working")

actions.send_keys(Keys.TAB * 3)
actions.send_keys(Keys.ENTER)
actions.perform()
print("Navigate Post area Successfully ")


login()
driver.get("https://www.facebook.com/groups/402353916617590/permalink/1630582000461436/")
navigatePagePostAria()

我还将 driver.get("https://facebook.com") 迁移到 login 方法中,因为这更多地属于基本初始化selenium webdriver 本身

关于python - Keys.BACK_SPACE 或 Keys.TAB 发送 key 不适用于 facebook selenium python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67564250/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com