Python使用selenium,value_of_css_property输出和检查不同

时间:2017-02-08 13:11:09

标签: python selenium

我试图确定哪个文字是粗体,哪个不是,但所有属性都恢复正常,我希望有人能解释为什么,以及最好的解决方法它或绕过它。

我的python代码:

from selenium import webdriver 
urls = (
"http://www.espn.co.uk/rugby/match?gameId=290767&league=180659",
"http://www.espn.co.uk/rugby/match?gameId=290768&league=180659")

driver = webdriver.Chrome("c:/temp/chromedriver_win32/chromedriver.exe")
driver.set_window_size(681,768)

for url in urls:
    driver.get(url)
    ft = driver.find_element_by_class_name("game-time")
    if (ft.text) == "FT":
        #Opens player stats window
        driver.find_element_by_xpath("""//*[@id="global-nav-tertiary"]/div/ul/li[6]""").click()

        posts = driver.find_elements_by_xpath("""//*[@id="main-container"]/div/div/div[1]/article/div/div[1]/div/div/div/table/tbody[1]/tr/td""")
        for post in posts:
            print post.value_of_css_property('font-weight')

        driver.find_element_by_xpath("""//*[@id="main-container"]/div/div/div[1]/article/header/div/button[2]/span[2]""").click()

        posts = driver.find_elements_by_xpath("""//*[@id="main-container"]/div/div/div[1]/article/div/div[1]/div/div/div/table/tbody[1]/tr/td""")
        for post in posts:
            print post.value_of_css_property('font-weight')

    else:
        #skipping all games that haven't played yet
        pass
#closing driver
driver.quit()

输出: 正常

检查中的元素:

<span class="name" style="font-weight:bold;" data-reactid=".88mmgctyio.2.0.0.0.$sq.1.0.0.0.0.0.2.$2.0.0.0.$name.0">Jonathan Davies, C </span>

1 个答案:

答案 0 :(得分:0)

您正在尝试获取CSS元素的td属性,而您应该处理span(它们不是相同的元素 - 每个元素都有自己的'font-weight'属性) :

for post in driver.find_elements_by_xpath('//span[@class="name"]'):
    print post.value_of_css_property('font-weight')