2016-09-14 122 views
0

我的目标是使用Selenium for Python自动完成在线账单支付。使用Selenium Webdriver浏览网站

登录成功使用的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") 

但仍然一无所获。我应该尝试哪些其他的事情?

错误:

回溯(最近通话最后一个): 文件 “/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 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中 '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(消息,屏幕,堆栈跟踪) selenium.common.exceptions.NoSuchElementException:消息:无法找到元件:在FirefoxDriver.prototype.findElementInternal_ (文件::{ “方法”: “链接文本”, “选择器”: “比尔&付款”}

堆栈跟踪/// TMP/tmps7uj9u0l /扩展/[email protected]/components/driver-component.js:10770) at fxdriver.Timer.prototype.setTimeout/< .notify(file:///tmp/tmps7uj9u0l/extensions/[email protected]/components/ driver-component.js:625)

+0

您是否收到任何错误? – alecxe

+0

你能否分享这个链接HTML? –

+0

只是增加了错误。谢谢 – pythonomicon

回答

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解释器打开。

如果问题似乎是硒不会等待足够长的页面加载,你总是可以尝试像一个wait方法:

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() 
+0

我无法找到该元素的ID,所以我使用find_element_by_partial_link_text方法并使用它。现在,在找出如何复选框.... – pythonomicon

+0

很高兴你知道了! – Brydenr

0

尝试使用

click_link =驱动程序

click_link.click()

1

文档通常是太技术,我搞不懂,但它是相当straig .find_element_by_xpath(“在线账单支付”链接的eg.xpath)用于Selenium Python绑定的htforward。

因为在链接文字一些格式化,我用了部分链接文本的方法和它的工作。从the documentation

例如:

continue_link = driver.find_element_by_partial_link_text('Conti') 
相关问题