[每次我加载python脚本时,每次登录前都要手动更改Facebook登录按钮和弹出按钮的Xpath。如何更改此设置,这样我就不必手动输入XPath?
from selenium import webdriver
from time import sleep
class TinderBot():
def __init__(self):
self.driver = webdriver.Chrome()
def login(self):
self.driver.get('https://tinder.com')
sleep(3)
#fbxpath = //button[@type = 'button' and @aria-label = 'Log in with Facebook']
fb_btn = self.driver.find_element_by_xpath('//*[@id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button')
fb_btn.click()
base_window = self.driver.window_handles[0]
self.driver.switch_to_window(self.driver.window_handles[1])
fbemail = self.driver.find_element_by_xpath('//*[@id="email"]')
fbemail.send_keys('[email protected]')
fbpass = self.driver.find_element_by_xpath('//*[@id="pass"]')
fbpass.send_keys('BlackToast5')
login_btn = self.driver.find_element_by_xpath('//*[@id="u_0_0"]')
login_btn.click()
self.driver.switch_to_window(base_window)
popup1 = self.driver.find_element_by_xpath('//*[@id="modal-manager"]/div/div/div/div/div[3]/button[1]')
popup1.click()
popup2 = self.driver.find_element_by_xpath('//*[@id="modal-manager"]/div/div/div/div/div[3]/button[1]')
popup2.click()
bot = TinderBot()
bot.login()
这是我得到的错误:
File "tind_bt.py", line 16, in login
fb_btn = self.driver.find_element_by_xpath('//*[@id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button')
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button"}
(Session info: chrome=80.0.3987.106)
你很近。要在文本为使用Facebook登录的元素上调用click()
,您需要诱导WebDriverWait,直到element_to_be_clickable()
到达子级<span>
,并且可以使用以下任一[C0 ]:
CSS_SELECTOR
使用WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type = 'button'][aria-label = 'Log in with Facebook'] span"))).click()
:
XPATH
注:您必须添加以下导入:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@type = 'button' and @aria-label = 'Log in with Facebook']//span"))).click()
浏览器快照:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
也许您可以实现WebDriverWait,看看是否可以解决问题。
这是显式等待的Python文档-Selenium WebDriver wait till element is displayed