【发布时间】:2020-08-30 20:45:00
【问题描述】:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup as b
import time
class Login:
def __init__(self, driver, username, password):
self.driver = driver
self.username = username
self.password = password
def signin(self):
print('opening')
self.driver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
uid = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#react-root > section > main > article > div.rgFsT > div:nth-child(1) > div > form > div:nth-child(2) > div > label > input')))
uid.click()
uid.send_keys(self.username)
pswd = self.driver.find_elements_by_css_selector('#react-root > section > main > article > div.rgFsT > div:nth-child(1) > div > form > div:nth-child(3) > div > label > input')
pswd.click()
pswd.send_keys(self.password)
btn = self.driver.find_elements_by_css_selector('#react-root > section > main > article > div.rgFsT > div:nth-child(1) > div > form > div:nth-child(4)')
btn.click()
time.sleep(3)
【问题讨论】:
-
我们需要查看您的源代码。您能否尝试在 Chrome 开发工具中粘贴您的 CSS 选择器并查看该元素是否突出显示。我猜要么元素未在 10 秒内加载,要么 CSS 选择器不正确。是否也可以提供HTML?所以我们可以看看它,看看我们是否可以缩短选择器
标签: python selenium selenium-webdriver selenium-chromedriver browser-automation