1

I'm currently looking to pull the specific typography a company uses on stylify me (e.g. for http://stylifyme.com/?stylify=uber.com i want to pull "UberMove, 'Open Sans', 'Helvetica Neue', Helvetica, sans-serif, normal, 52px, 56px, #000000"). However, I'm running into issues when it comes to finally pulling the text - the text shows in the html but does not appear when i try to pull the text. I've tried pulling both the Inner HTML and just the text - see example code and text below.

page=webdriver.Chrome('/Downloads/chromedriver.exe')
page.get('http://stylifyme.com/')
website_finder=page.find_element_by_id('input-stylify')
website_finder.send_keys('www.bcg.com')
website_finder.submit()

#try 1:
print(page.find_element_by_id("result-header-1-dt").text)
#output 1: "Header 1: Font, Style, Size, Leading, Colour"

#try 2
print(page.find_element_by_xpath('/html/body/div[1]/table/tbody/tr[1]/th/strong').get_attribute("innerHTML"))
#output 2: "Header 1:"


HTML code:

<th id="result-header-1-dt" class="first" scope="row"><strong style="opacity: 1;">
UberMove, 'Open Sans', 'Helvetica Neue', Helvetica, sans-serif, normal, 52px, 56px, #000000
</strong> <span style="opacity: 1;">Font, Style, Size, Leading, Colour</span></th>

Any help would be greatly appreciated!

1 Answers1

0

As pguardiario mentioned, the solution is to wait for the element to be loaded. Using time.sleep(5) works fine much of the time, but often using a WebDriverWait can work better. time.sleep sleeps for a set amount of time, which can lead to unnecessary pauses in running the script, or failures if a page takes a really long time to load. WebDriverWait helps keep scripts running by finishing once the element is found. If the element is never found, then an Exception will be thrown.

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait

driver = ...
max_wait_time = 5
selector = ...
by = By.XPATH  # Or By.ID, By.CSS_SELECTOR, etc.

try:
    WebDriverWait(driver, max_wait_time).until(ec.presence_of_element_located((by, selector)))
except TimeoutException:
    print("Failed to find an element with", selector, "in", max_wait_time, "seconds)

Brydenr
  • 798
  • 1
  • 19
  • 30