登录网站后,我无法导航。并且程序不会抛出错误。我怀疑是因为换了相框,但没能解决问题。
RedditBot.driver.get(self.targetUrl) #working
RedditBot.driver.get("https://www.reddit.com/")
login=RedditBot.driver.find_element_by_link_text("Log In")
login.click()
RedditBot.driver.switch_to_frame(RedditBot.driver.find_element_by_tag_name('iframe'))
username_in = RedditBot.driver.find_element_by_xpath("//*[@id='loginUsername']")
self.typing(username_in, "username")
pass_in = RedditBot.driver.find_element_by_xpath("//*[@id='loginPassword']")
self.typing(pass_in, "pass")
pass_in.send_keys(Keys.ENTER)
RedditBot.driver.switch_to_default_content()
RedditBot.driver.get(self.targetUrl) #not working
我使用下面的代码显式等待来打印当前的URL:
代码:
driver = webdriver.Chrome(options = options)
driver.maximize_window()
driver.get("https://www.reddit.com/")
wait = WebDriverWait(driver, 10)
print(driver.current_url)
login = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href^='https://www.reddit.com/login/?']")))
login.click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src*='login']")))
wait.until(EC.element_to_be_clickable((By.ID, "loginUsername"))).send_keys("your user name")
wait.until(EC.element_to_be_clickable((By.ID, "loginPassword"))).send_keys("your password")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[class^='AnimatedForm__submitButton']"))).click()
driver.switch_to.default_content()
wait.until(EC.element_to_be_clickable((By.ID, "header-search-bar"))).send_keys("Python" + Keys.RETURN)
sleep(5)
print(driver.current_url)
O/p:
https://www.reddit.com/
https://www.reddit.com/search/?q=Python
Process finished with exit code 0