title: How to return "N/A" when driver can't find xpath of an element?
body: <p>I have this specific url from a list of urls and this specifc one has a different xpath format than other urls in terms of where &quot;Brand Name&quot; is located.
  So I wanted to return something like a string &quot;N/A&quot; or None but I get the error of</p>
  <pre><code>AttributeError: 'NoneType' object has no attribute 'text'
  or
  AttributeError: 'str' object has no attribute 'text'
  </code></pre>
  <p>which is probably from calling brand_name.text on None or calling brand_name.text on &quot;N/A&quot;</p>
  <p>I can't change calling brand_name.text in my row = {} as that does its job perfectly with other urls
  I also don't want to make another code finding the xpath of the brand_name in this specific url as there are going to be other urls with other different formats for the brand_name.</p>
  <p>What would I need to do to make brand_name return something like &quot;N/A&quot; when the driver can't find xpath to it?</p>
  <p>Below is my code</p>
  <pre><code>import pandas as pd
  import csv
  from bs4 import BeautifulSoup
  from selenium import webdriver
  import os
  from selenium.common.exceptions import NoSuchElementException
  from selenium.common.exceptions import StaleElementReferenceException
  
  chromedriver = &quot; - path to chrome driver -&quot;
  driver = webdriver.Chrome(chromedriver)
  rows = []
  
  url = https://www.amazon.com/BEAKEY-Foundation-Blending-Flawless-Multi-colored/dp/B01F36JEXE/ref=sr_1_22?dchild=1&amp;keywords=cosmetics&amp;qid=1625014752&amp;sr=8-22
  
  driver.get(url)
      
      # BRAND NAME
      try:
          brand_name = driver.find_element_by_xpath('//*[@class=&quot;a-spacing-small&quot;][.//*[contains(.,&quot;Brand&quot;)]]/td[@class=&quot;a-span9&quot;]/span')
      except (NoSuchElementException, StaleElementReferenceException):
          brand_name = None
      
      # SELLER
      seller = driver.find_element_by_xpath('(//span[@class=&quot;a-truncate-cut&quot;]/span[@class=&quot;tabular-buybox-text&quot;])[2]')
      
      # DELIVERY DATE
      delivery_date = driver.find_element_by_xpath('//div[@id=&quot;mir-layout-DELIVERY_BLOCK&quot;]/div[@class=&quot;a-spacing-base&quot;]/b')
     
      
      row = { 'Brand Name': brand_name.text,
              'Seller': seller.text,
              'Delivery Date': delivery_date.text
            }
      
      if brand_name is not None:
            row['Brand Name'] = brand_name.text
      else:
            row['Brand Name'] = &quot;N/A&quot;
      
      
      
      rows.append(row)
  
  driver.close()
  df = pd.DataFrame(rows)
  df.to_csv('Result2.csv', index=False)
  </code></pre>