简体   繁体   English

无法让Webelement在Python 2.7中打印

[英]Can't get webelement to print in Python 2.7

So this is the code that I am running: 这就是我正在运行的代码:

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

When I try to print instance , the output I get is 当我尝试打印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

Not sure why I am getting a blank line as the output. 不知道为什么我得到一个空行作为输出。 If I print it out as, print type(instance) , I get unicode back as the type. 如果我将其打印为print type(instance) ,则返回unicode作为类型。

I assume the element.text is empty or None and there is nothing to print. 我假设element.text为空或None ,没有要打印的内容。

Without knowing the actual data it is impossible to say. 不知道实际数据就不可能说。

You can test it by 您可以通过以下方式进行测试

if not instance:
    print "No data" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM