2016-09-14 98 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') 
相關問題