2017-06-20 160 views
0

我弄到:如何獲取WebElement的HTML代碼或WebElement的屬性?

def picture(self): 
    path = "/home/user/Desktop/cat.jpg" 
    path2 = "/home/user/Desktop/puppy.jpg" 
    pictureName = self.driver.find_element_by_xpath('//*[@id="easSettingAvatar"]') 
    self.captureScreen('picture', 1) 
    editPhoto.send_keys(path) 
    time.sleep(5) 
    print(pictureName) 
    self.captureScreen('picture', 2) 
    editPhoto.send_keys(path2) 
    time.sleep(5) 
    print(pictureName) 

所以基本上,當我選擇其中包含SRC元素的CSS或XPath,它打印完全不同的信息,如:

<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="bba710d8-ac5e-4e96-b946-14b3eda53cca", element="6d243513-8109-495e-a7a4-e4d218bbbd9a")> 

+0

當然'driver.find_element_by_xpath'返回一個'WebElement'對象。你期待什麼樣的結果?未解析的HTML源代碼? –

+0

我期待看到這個:或只是src – NatNat

+0

可能重複的[使用Python獲取Selenium WebDriver中的WebElement的HTML源代碼](https://stackoverflow.com/questions/7263824/get-html-source-of-webelement -in-selenium-webdriver-using-python) –

回答

0

你必須打印WebElement的屬性而不是打印WebElement。

def picture(self): 
    path = "/home/user/Desktop/cat.jpg" 
    path2 = "/home/user/Desktop/puppy.jpg" 
    pictureName = self.driver.find_element_by_xpath('//*[@id="easSettingAvatar"]') 
    self.captureScreen('picture', 1) 
    editPhoto.send_keys(path) 
    time.sleep(5) 
    print(pictureName.get_attribute('outerHTML')) 
    print(pictureName.get_attribute('src')) 
    self.captureScreen('picture', 2) 
    editPhoto.send_keys(path2) 
    time.sleep(5) 

結果應該是

<img id="easSettingAvatar" src="eas.eightdevelopment.net/getAvatar/…; 
eas.eightdevelopment.net/getAvatar/…