首页 文章

使用Selenium Webdriver浏览网站

提问于
浏览
0

我的目标是使用Selenium for Python自动化在线账单支付 .

使用此驱动程序使用此代码登录成功:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://website.com/Home')
emailElem = browser.find_element_by_id('UserName') #finds login username field 
emailElem.send_keys('username') #enter the username
passwordElem = browser.find_element_by_id('UserPassword') #finds pw field
passwordElem.send_keys('password') #enters pw
passwordElem.submit() #presses submit button

登录后,将加载新页面,我的下一步是单击链接 . 代码:

browser.implicitly_wait(3) #allow new page to load (also tried 5 seconds)
click_link = browser.find_element_by_link_text("Bill & Payment")
click_link.click()

没有任何反应 . 没有导航到“帐单和付款”页面 . 实际链接中有一个 <BR> 标记,所以我也尝试包含标记:

click_link = browser.find_element_by_link_text("Bill &<BR>Payment")

但仍然没有 . 我还应该尝试一些其他的事情吗?

错误:

回溯(最近一次调用最后一次):文件“/home/captain/.PyCharmEdu30/config/scratches/scratch_1.py”,第12行,在click_link = browser.find_element_by_link_text(“账单和付款”)#单击下一页的链接文件“/home/captain/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”,第317行,在find_element_by_link_text中返回self.find_element(by = By.LINK_TEXT,value = link_text )文件“/home/captain/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”,第752行,在find_element'value':value})['value']文件“/home/captain/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”,第236行,执行self.error_handler.check_response(响应)文件“/ home / captain / .local / lib / python3.5 / site-packages / selenium / webdriver / remote / errorhandler.py“,第192行,在check_response中引发exception_class(message,screen,stacktrace)selenium.common.exceptions.NoSuchElementException:消息:无法locate元素:{“method”:“链接文本”,“selec tor“:”Bill&Payment“} Stacktrace:在fxdriver.Timer的FirefoxDriver.prototype.findElementInternal_(文件:///tmp/tmps7uj9u0l/extensions/fxdriver@googlecode.com/components/driver-component.js:10770) . prototype.setTimeout / < . notify(file:///tmp/tmps7uj9u0l/extensions/fxdriver@googlecode.com/components/driver-component.js:625)

3 回答

  • 1

    您遇到的错误是您要查找的元素不在页面中 . 根据我对Selenium的经验,我发现css selectors通常最适合与网站互动 . 您还可以从命令行运行python,以通过以下方式测试元素是否具有良好的挂钩值:

    from selenium import webdriver
    browser = webdriver.Firefox()
    browser.get('https://website.com/Home')
    element = "what you want to test here"
    driver.find_element_by_id(element).click()
    

    只要你保持python解释器打开,你就可以继续改变元素的值并运行这些行 .

    如果问题似乎是Selenium没有等待足够的时间来加载页面,你总是可以尝试等待方法,如:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as ec
    
    time = 10  # Wait for 10 seconds
    by = By.CSS_SELECTOR  # The type of hook the element is
    hook = "css.selector.here"  # The value for the hook (could also be xpath, name, id, etc.)
    # Waits until either the element specified by variables by and hook is     
    # In the page, or the value of time seconds has passed.
    WebDriverWait(self.driver, time).until(ec.presence_of_element_located((by, hook)))
    driver.find_element(by, hook).click()
    
  • 1

    文档对我来说通常太技术化了,但对于Selenium Python Bindings来说它非常简单 .

    因为链接文本中有一些格式,我使用部分链接文本方法,它工作 .

    例子来自the documentation

    continue_link = driver.find_element_by_partial_link_text('Conti')
    
  • 0

    尝试使用

    click_link = driver.find_element_by_xpath(“在线账单支付”链接的eg.xpath)

    click_link.click()

相关问题