【问题标题】:Automatically logged out when searching using selenium使用 selenium 搜索时自动注销
【发布时间】:2016-06-20 15:22:38
【问题描述】:

我正在使用 selenium 自动搜索网站。 这是我的代码。

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


def init_driver():
    driver = webdriver.Firefox()
    driver.wait = WebDriverWait(driver, 5)
    return driver


def lookup(driver, query):
    driver.get("www.example.com/search.php")
    try:
        button = driver.wait.until(EC.element_to_be_clickable(
            (By.ID, "ad")))
        button.click()
        # query = "telugu"
        # box = driver.wait.until(EC.element_to_be_clickable(
        #     (By.NAME, "keytext")))
        # box.send_keys(query)
        submit_text = "SEARCH"
        submit_button = driver.wait.until(EC.element_to_be_clickable(
            (By.XPATH, "(//*[contains(text(), '" + submit_text + "')] | //*[@value='" + submit_text + "'])")))
        submit_button.click()
        print 'aaaa'
    except TimeoutException:
        print("Box or Button not found in google.com")


if __name__ == "__main__":
    driver = init_driver()
    lookup(driver, "Selenium")
    time.sleep(1000)
    driver.quit()

我在 Firefox 中登录该站点,然后运行此脚本。

预期的网址正确打开(此网址仅在我登录时打开),点击搜索按钮。

结果会显示几秒钟,然后显示登录屏幕。

但我实际上并没有退出,因为如果我在不同的选项卡中打开此站点,我已登录。

我不理解 selenium 的这种奇怪行为。网站是否以某种方式检测到它是试图点击的机器人?

谁能告诉我如何解决或绕过这个问题?

【问题讨论】:

  • 手动尝试相同的步骤。这可能是产品缺陷。
  • 我手动试了一下,效果很好!!

标签: python selenium firefox


【解决方案1】:

我怀疑您的产品出现了预期行为。您提到您手动打开浏览器并登录,然后运行脚本。当您的脚本运行时,Selenium 将打开一个带有新配置文件的新浏览器,这意味着它不会有任何可能用于您网站上的身份验证的 cookie。

您必须修改您的脚本,使其在尝试执行剩余步骤之前使用有效凭据登录。

【讨论】:

    猜你喜欢
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    相关资源
    最近更新 更多