我想使用selenium色度驱动器来填充https://www.asiamiles.com/en/login.html中的登录名& pw,但是,我发现只有当我没有将.add_argument设置为“-无头”时,.find_elemen_by_xpath才能工作。
我猜如果我在后台运行色度驱动器,输入就会隐藏起来.
asia_miles_url = 'https://www.asiamiles.com/en/login.html'
driver = ws_functions.get_ChromeDriver(()) #it works
# driver = ws_functions.get_ChromeDriver(("--headless")) #it doesn't work
driver.implicitly_wait(30)
driver.get(asia_miles_url)
time.sleep(8)
driver.find_element_by_xpath('/html/body/div[3]/div/div/div/div[2]/div/form/div[2]/div[1]/div[1]/div/input').send_keys('abc')
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[3]/div[2]/div/div/div[2]/div/form/div[2]/div[1]/div[2]/div/input').send_keys(password)
time.sleep(0.5)
driver.find_element_by_xpath('/html/body/div[3]/div[2]/div/div/div[2]/div/form/div[2]/div[1]/div[3]/div[3]/button').click()我在ws_functions中的自定义函数
#Get ChromeDriver
def get_ChromeDriver(*headless): #turple headless = null -> Chrome runs in foregound
chrome_options = Options()
if headless[0] == "--headless":
chrome_options.add_argument("--headless")
chrome_options.add_argument("window-size=1920,1080")
return webdriver.Chrome(executable_path=settings.chromedriver,options=chrome_options)有人知道为什么吗?谢谢。
发布于 2021-02-24 10:30:48
首先,这里有一个您可以使用的简单xpath。下一个问题是,当运行无头时,站点可能会发现您的机器人。
wait = WebDriverWait(driver, 10)
driver.get('https://www.asiamiles.com/en/login.html')
wait.until(EC.element_to_be_clickable((By.XPATH,"(//input[@id='username'])[2]"))).send_keys('abc')
wait.until(EC.element_to_be_clickable((By.XPATH,"(//input[@id='password'])[2]"))).send_keys(password)
wait.until(EC.element_to_be_clickable((By.XPATH,"(//button[.='Log in'])[3]"))).click()导入
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC如果你打印driver.page_source
<html><head>
<title>Access Denied</title>
</head><body>
<h1>Access Denied</h1>
You don't have permission to access "http://www.asiamiles.com/en/login.html" on this server.<p>
Reference #18.869a3b17.1614165148.2921fb4b
</p></body></html>https://stackoverflow.com/questions/66346610
复制相似问题