<jupyter_start><jupyter_text>---<jupyter_code>md = webdriver.Chrome() #  오픈
md.get('https://cloud.google.com/vision/') # 해당 주소로 이동
md.set_window_size(900,700) # size setting
md.execute_script("window.scrollTo(0, 1000);") # 브라우저 스크롤 이동<jupyter_output><empty_output><jupyter_text>현재 윈도우 위치 저장<jupyter_code>main = md.current_window_handle<jupyter_output><empty_output><jupyter_text>새로운 탭 오픈 (포커스는 변경x)<jupyter_code>md.execute_script("window.open('https://www.google.com');")
windows = md.window_handles # 윈도우 체크
windows<jupyter_output><empty_output><jupyter_text>switch_to_window : focus 변경<jupyter_code>md.switch_to_window(windows[1])
md.get('https://www.naver.com')
md.switch_to_window(main)
md.execute_script('location.reload()') #새로고침<jupyter_output><empty_output><jupyter_text>control alert<jupyter_code>md.execute_script('alert("selenium test")')
alert = md.switch_to_alert()
print(alert.text)
alert.accept()
md.execute_script('alert("selenium test")')
md.switch_to_alert().accept()
md.execute_script("confirm('confirm?')")
# alert = md.switch_to_alert() 
print(alert.text)
# alert.accept()
alert.dismiss()<jupyter_output>confirm?<jupyter_text>input key & button<jupyter_code>md.switch_to_window(windows[1])
md.find_element_by_css_selector('#query').send_keys('test')
md.find_element_by_css_selector(".ico_search_submit").click()<jupyter_output><empty_output><jupyter_text>close driver<jupyter_code>md.close() # one for one
for i in md.window_handles:
    md.switch_to_window(i)
    md.close()<jupyter_output><empty_output><jupyter_text>---  file uploadhttps://visual-recognition-demo.ng.bluemix.nethttps://cloud.google.com/vision/<jupyter_code>cr = webdriver.Chrome()
cr.get('https://cloud.google.com/vision/')<jupyter_output><empty_output><jupyter_text>iframe의 경우 포커스 이동이 필요함<jupyter_code>iframe = cr.find_element_by_css_selector('#vision_demo_section > iframe ')
cr.switch_to_frame(iframe)
# Switch back default content
# cr.switch_to_default_content() #아이프레임 밖으로 포커스 이동
path = !pwd #현재 디렉토리위치 리스트
print(type(path), path)
file_path = path[0] + "/screenshot_element.png"
cr.find_element_by_css_selector('#input').send_keys(file_path) # 파입 업로드
cr.find_element_by_css_selector('#safeSearchAnnotation').click()<jupyter_output><empty_output><jupyter_text>safe search 항목 점수 출력<jupyter_code>a = cr.find_elements_by_css_selector('#card div.row.style-scope.vs-safe') 
for i in a:
    print(i.text)<jupyter_output>Adult Very Unlikely
Spoof Unlikely
Medical Very Unlikely
Violence Very Unlikely
Racy Very Unlikely<jupyter_text>---  한번에 실행<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] + "/screenshot_element.png"
driver.find_element_by_css_selector("#input").send_keys(file_path)
time.sleep(15) # 이미지를 업로드하고 데이터를 분석하는 시간
driver.find_element_by_css_selector("#safeSearchAnnotation").click()
a = driver.find_elements_by_css_selector('#card div.row.style-scope.vs-safe')

for i in a:
    print(i.text)
driver.close()<jupyter_output>Adult Very Unlikely
Spoof Unlikely
Medical Very Unlikely
Violence Very Unlikely
Racy Very Unlikely<jupyter_text>element 체크하면서 실행<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] + "/screenshot_element.png"
driver.find_element_by_css_selector("#input").send_keys(file_path)

selector = '#card div.row.style-scope.vs-safe'
sec, limit_sec = 0, 10
while True:
    sec += 1
    print("{}sec".format(sec))
    time.sleep(1)
    
    # element 확인
    if check_element(driver, selector):
        driver.find_element_by_css_selector("#safeSearchAnnotation").click()
        a = driver.find_elements_by_css_selector('#card div.row.style-scope.vs-safe')
        for i in a:
            print(i.text)
        driver.close()
        break;
        
    # limit_sec가 넘어가면 에러 처리
    if sec + 1 > limit_sec:
        print("error")
        driver.close()
        break;<jupyter_output>1sec
2sec
3sec
4sec
Adult Very Unlikely
Spoof Unlikely
Medical Very Unlikely
Violence Very Unlikely
Racy Very Unlikely
{'fraction_non_alphanumeric': 0.1319854611540209, 'fraction_numerical': 0.006360745115856429, 'mean_word_length': 6.1477272727272725, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 47, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '667885', 'license': 'MIT', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1822, 'n_words': 241, 'path': 'Past/DSS/Programming/Scraping/180220_selenium.ipynb', 'repo_name': 'Moons08/TIL', 'size': '4402'}