2015-06-30 36 views
1

打印所以這是我運行代碼:無法取得webelement在Python 2.7

from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as EC 
from selenium import webdriver 
from selenium.webdriver.support import ui 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.common.keys import Keys 
current_price = [] 
def page_is_loaded(driver): 
    return driver.find_element_by_tag_name("body") != None 
driver = webdriver.Firefox() 
driver.get("https://marketsworld.com/") 
wait = WebDriverWait(driver, 10) 
wait.until(page_is_loaded) 
email_field = driver.find_element_by_id("user_login_email") 
email_field.send_keys("myusername") 
password_field = driver.find_element_by_id("user_login_password") 
password_field.send_keys("mypass") 
password_field.send_keys(Keys.RETURN) 
element = driver.find_element_by_class_name("market_value") 
instance = element.text 
print instance 

當我嘗試打印instance,輸出我得到的是

/usr/bin/python2.7 /home/phuhzy/PycharmProjects/pychar_test/test2.py 

(I don't get any output here. Only a blank line.) 

Process finished with exit code 0 

不確定爲什麼我得到一個空行作爲輸出。如果我將它打印爲print type(instance),我將unicode作爲類型返回。

+0

測試它嘗試打印'再版(實例)',它更像是在交互式Python提示符輸出。順便說一句:升級到Python 3! –

+0

謝謝你的幫助。我是一個白癡,只從網頁上有超過一個類名稱market_value的網頁調用market_value的第一個實例。我對python還是一個相當新的東西,一旦我有了基礎知識,就會升級到3。 – dvitt90

回答

0

我假設element.text是空的或None並且沒有什麼可打印的。

不知道實際的數據是不可能的。

您可以通過

if not instance: 
    print "No data" 
+0

謝謝你的幫助,後來我發現如果我把它放在一個列表中,然後打印出列表,我會得到你'' 。這意味着這個unicode是一個空字符串,後來我意識到我是一個白癡,並且只從網頁上的market_value的第一個實例調用,這個網頁在網頁上有多個類名稱market_value,我通過調用element = driver.find_elements_by_class_name(「market_value」)|然後打印len的元素,發現有4個market_value.3的實例,它有一個空字符串/沒有值和一個有我正在尋找的值。 – dvitt90