【问题标题】:Navigating a Website with Selenium Webdriver使用 Selenium Webdriver 导航网站
【发布时间】:2016-09-14 16:56:54
【问题描述】:

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

使用带有此代码的 Webdriver 成功登录:

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")

但还是一无所获。我还应该尝试哪些其他事情?

错误:

Traceback(最近一次调用最后一次): 文件“/home/captain/.PyCharmEdu30/config/scratches/scratch_1.py”,第 12 行,在 click_link = browser.find_element_by_link_text("Bill & Payment")#点击下一页链接

文件“/home/captain/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”,第 317 行,在 find_element_by_link_text return 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 '值': 值})['值']

文件“/home/captain/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”,第 236 行,在执行中 self.error_handler.check_response(response)

文件“/home/captain/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py”,第 192 行,在 check_response 引发异常类(消息、屏幕、堆栈跟踪) selenium.common.exceptions.NoSuchElementException:消息:无法定位元素:{"method":"link text","selector":"Bill & Payment"}

堆栈跟踪: 在 FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmps7uj9u0l/extensions/fxdriver@googlecode.com/components/driver-component.js:10770) 在 fxdriver.Timer.prototype.setTimeout/<.notify>

【问题讨论】:

  • 您是否遇到任何错误?
  • 你能把这个链接也分享给 HTML 吗??
  • 刚刚添加了错误。谢谢
  • 请提供该链接标签的来源

标签: python-3.x selenium


【解决方案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()

【讨论】:

  • 我无法找到该元素的 ID,因此我使用了 find_element_by_partial_link_text 方法,它成功了。现在,来了解如何勾选复选框......
  • 很高兴你知道了!
【解决方案2】:

文档对于我来说通常过于技术性,但对于 Selenium Python 绑定来说非常简单。

因为链接文本中有一些格式,所以我使用了部分链接文本方法并且它有效。

来自the documentation的示例:

continue_link = driver.find_element_by_partial_link_text('Conti')

【讨论】:

    【解决方案3】:

    尝试使用

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

    click_link.click()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-16
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      相关资源
      最近更新 更多