<jupyter_start><jupyter_text>selenium & webdriver - 2- file uploading through selenium- use API<jupyter_code>import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://cloud.google.com/vision')<jupyter_output><empty_output><jupyter_text>change iframe- upload image on google vision api test page and checking the result- the upload area is set as `iframe`, so we'll move the foucs to `iframe`<jupyter_code>iframe = driver.find_element_by_css_selector("#vision_demo_section iframe")
driver.switch_to_frame(iframe)<jupyter_output><empty_output><jupyter_text>file upload<jupyter_code># check current path
path = !pwd
path
file_path = path[0] + "/practice1.png"
driver.find_element_by_css_selector("#input").send_keys(file_path)
driver.find_element_by_css_selector("#labelAnnotations").click()
driver.find_elements_by_css_selector('.style-scope .vs-labels .name')[0].text<jupyter_output><empty_output><jupyter_text>running all the code in one attempt<jupyter_code>driver =  webdriver.Chrome()
driver.get('https://cloud.google.com/vision/')

iframe = driver.find_element_by_css_selector("#vision_demo_section iframe")
driver.switch_to_frame(iframe)
file_path = path[0] + "/practice1.png"
driver.find_element_by_css_selector("#input").send_keys(file_path)
time.sleep(10) # time allowance for uploading image and anaylzing
driver.find_element_by_css_selector("#webDetection").click()
result = driver.find_elements_by_css_selector('.style-scope .vs-web .name')[0].text
print(result)
driver.close()<jupyter_output>Performance<jupyter_text>Check element while running- apply try and except<jupyter_code>def check_element(driver, selector):
    try:
        driver.find_element_by_css_selector(selector)
        return True
    except:
        return False
driver =  webdriver.Chrome()
driver.get('https://cloud.google.com/vision/')

iframe = driver.find_element_by_css_selector("#vision_demo_section iframe")
driver.switch_to_frame(iframe)
file_path = path[0] + "/Drake.png"
driver.find_element_by_css_selector("#input").send_keys(file_path)

selector = '.vs-web .name'
sec, limit_sec = 0, 3 
while True:
    sec += 1
    print("{}sec".format(sec))
    time.sleep(1)
    
    # check element
    if check_element(driver, selector):
        driver.find_element_by_css_selector("#webDetection").click()
        result = driver.find_elements_by_css_selector(selector)[0].text
        print(result)
        driver.close()
        break;
        
    # if limit_sec exceeds, handle it 'except'
    if sec + 1 > limit_sec:
        print("error")
        driver.close()
        break;<jupyter_output>1sec
2sec
3sec
error
{'fraction_non_alphanumeric': 0.11807137433561124, 'fraction_numerical': 0.00721336370539104, 'mean_word_length': 5.458333333333333, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '503684', 'license': 'MIT', 'n_tokens_mistral': 897, 'n_tokens_neox': 862, 'n_words': 160, 'path': '2_python_Packages_Libraries/6_selenium/01_reviewNote_selenium/02_selenium_webdriver_file_uploading.ipynb', 'repo_name': 'lucaseo/TIL', 'size': '2634'}