一直在尝试我能想到的所有选择器和属性,以便定位并单击网站主页上的注册按钮......但都无济于事。 请参阅网站:https://onepetro.org
一旦网络驱动程序启动,所有事件都会执行,直到到达单击按钮的位置,然后我得到一个
NoSuchElementException
。
任何建议或建议将不胜感激。谢谢
def user_auth(self, username:str, password:str):
''' Authenticate the user with the given username and password\n
Returns the captcha status, login status and username(if logged-in)'''
if not self.at_homepage:
self.go_to_homepage()
self.find_element(By.CSS_SELECTOR, '.dropdown-toggle.signin').click() # toggles the sign in dropdown
WebDriverWait(self, 5).until(
EC.presence_of_element_located((By.ID, "user_LoginFormPopup")),
EC.presence_of_element_located((By.ID, "pass_LoginFormPopup"))
)
username_field = self.find_element(By.ID, "user_LoginFormPopup")
username_field.clear()
username_field.send_keys(username + Keys.TAB)
password_field = self.find_element(By.ID, "pass_LoginFormPopup")
password_field.clear()
password_field.send_keys(password)
WebDriverWait(self, 30).until(
EC.presence_of_element_located((By.XPATH, '//iframe[@title="reCAPTCHA"]')), # Wait for the reCAPTCHA to load
EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#sign-in-dropdown > div > div > div.login-form__section-wrap > div:nth-child(1) > div:nth-child(7) > button'))
)
self.solve_captcha()
self.find_element(By.CSS_SELECTOR, '#sign-in-dropdown > div > div > div.login-form__section-wrap > div:nth-child(1) > div:nth-child(7) > button').click() # Click the sign in button
# self.execute_script("document.querySelector('.signin-button').click();") # Click the sign in button
try:
# user_menu = self.find_element(By.XPATH, '//*[@id="sign-in-dropdown"]/div')
_ = self.find_element(By.XPATH, '//*[@id="sign-in-dropdown"]/div/div/div[2]/div[1]/div[2]') # Check for presence of error message
except NoSuchElementException as e:
self.logged_in = True; self.username = username
else:
self.logged_in = False
return self.captcha_status, self.logged_in, self.username
尝试使用几个选择器,包括 XPATH、NAME、CLASS、ID 和 CSS 选择器本身,但都产生相同的
NoSuchElementException
。我仍然很困惑为什么没有一个起作用。
我相信问题可能与切换到
iframe
来解决验证码有关,但发布时不切换到默认内容,
因为注册按钮位于 iframe 之外。
我运行了一个理智流程来检查它,它对我来说工作得很好。
iframe = d.find_element(By.XPATH,'//iframe[@title="reCAPTCHA"]')
d.switch_to.frame(iframe)
d.find_element(By.CSS_SELECTOR,'.recaptcha-checkbox-border').click()
d.switch_to.default_content()
time.sleep(50) #I am manually solving the captcha here.
d.find_element(By.XPATH,'//button[text()="Sign In"]').click()
time.sleep(20) #To view the button click