我试图用selenium和python点击一个按钮,但它不工作

idfiyjo8  于 2023-05-05  发布在  Python
关注(0)|答案(1)|浏览(128)

嗨,我试图在selenium中使用python单击页面https://walmart.wd5.myworkdayjobs.com/login的按钮,我尝试了XPATH和id。使用了click,send_keys(Keys.enter),submit,但仍然没有点击。有人能帮帮我吗?enter image description here

options = Options()
options.add_experimental_option('excludeSwitches', ['enable-logging'])

driver = webdriver.Chrome(executable_path=r"C:\Users\user\Downloads\chromedriver_win32 (1)\chromedriver.exe,chrome_options=options")
# Load webpage
driver.get(url)
driver.maximize_window() # For maximizing window
driver.implicitly_wait(20) # gives an implicit wait for 20 seconds
# Find first name field and fill it out
email = driver.find_element(By.XPATH,"//*[@id='input-4']").send_keys("1234@gmail.com");
passw = driver.find_element(By.XPATH,"//*[@id='input-5']").send_keys("1233@1102");
sub = driver.find_element(By.XPATH,"//*[@id='input-5']").send_keys(Keys.TAB + Keys.ENTER)
eh57zj3b

eh57zj3b1#

首先,登录上述门户网站的正确URL是:https://walmart.wd5.myworkdayjobs.com/en-US/WalmartExternal/login
现在,你是对的,当我们尝试使用Selenium或任何其他自动化工具进行自动化登录时,它不允许我们简单地通过单击Sign In按钮来登录。
我注意到,当我们尝试在Sign In按钮上应用click()时,它确实执行了单击,但之后什么也没有发生。
我的朋友,在使用了很多不同的东西之后,我能够给你带来解决方案。
解决方案如下:

import time
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

options = ChromeOptions()
options.add_argument('--start-maximized')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36")

driver = Chrome(options=options)
wait = WebDriverWait(driver, 10)

url = "https://walmart.wd5.myworkdayjobs.com/en-US/WalmartExternal/login"
driver.get(url)

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'input[data-automation-id="email"]')))
email = driver.find_element(By.CSS_SELECTOR, 'input[data-automation-id="email"]')
email.send_keys('your_username')

password = driver.find_element(By.CSS_SELECTOR, 'input[data-automation-id="password"]')
password.send_keys('your_password')

submit = driver.find_element(By.CSS_SELECTOR, 'div[aria-label="Sign In"]')

hover = ActionChains(driver).move_to_element(submit)
hover.click().perform()

time.sleep(10)

需要注意的几点:
1.登录的正确URL是https://walmart.wd5.myworkdayjobs.com/en-US/WalmartExternal/login
1.我们需要等待Sign In框出现在页面上。
1.我们必须将user-agent传递给Chrome选项。
1.使用ActionChains成功执行单击以访问已登录的配置文件。简单的click()在这里不起作用。
我希望它能解决你的问题。干杯!

相关问题