在豆瓣主页的登录窗口,获取验证码图片位置坐标,代码执行后得到的结果与实际坐标差异太大,实际坐标如图1。网上的办法也找了,例如桌面缩放比设置为100%也设置了,如图2,还是一样的问题。
图1
图2
import time
from selenium import webdriver
if __name__ == '__main__':
username = '18983241561'
password = '654321'
browser = webdriver.Chrome(executable_path='C:\Program Files\Google\Chrome\Application\chromedriver.exe')
url = 'https://www.douban.com/'
browser.get(url)
browser.maximize_window()
time.sleep(3)
browser.switch_to.frame(browser.find_element_by_xpath("//iframe[@src='//accounts.douban.com/passport/login_popup?login_source=anony']"))
pass_choice = browser.find_element_by_xpath("//li[@class='account-tab-account']")
pass_choice.click()
username_ele = browser.find_element_by_xpath("//div[@class='account-tabcon-start']//div[@class='account-form-field']/input")
password_ele = browser.find_element_by_xpath("//input[@id='password']")
username_ele.send_keys(username)
password_ele.send_keys(password)
login_ele = browser.find_element_by_xpath("//a[@class='btn btn-account btn-active']")
login_ele.click()
time.sleep(5)
browser.switch_to.frame(browser.find_element_by_xpath("//iframe[@src='https://captcha.gtimg.com/1/template/drag_ele.html']"))
img = browser.find_element_by_xpath("//div[@class='tc-bg-img unselectable']")
location = img.location
print(type(location), location)
输出结果为:
<class 'dict'> {'x': 10, 'y': 62}
已找到原因,有时候真的佩服自己,作为刚入门学习的小白,在这上面提的大部分问题,最后还是靠自己解决了!
不知道你这个问题是否已经解决, 如果还没有解决的话: