docker_chrome_test.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import time
  2. import traceback
  3. import dataset
  4. from selenium import webdriver
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.support import expected_conditions as EC
  8. from selenium.webdriver.common.keys import Keys
  9. #import undetected_chromedriver as uc
  10. # options = webdriver.ChromeOptions()
  11. # options.add_argument('--no-sandbox')
  12. # options.add_argument('start-maximized')
  13. # options.add_argument('enable-automation')
  14. # options.add_argument('--disable-infobars')
  15. # options.add_argument('--disable-dev-shm-usage')
  16. # options.add_argument('--disable-browser-side-navigation')
  17. # options.add_argument("--remote-debugging-port=9222")
  18. # # options.add_argument("--headless")
  19. # options.add_argument('--disable-gpu')
  20. # options.add_argument("--log-level=3")
  21. # driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)
  22. options = webdriver.ChromeOptions()
  23. options.add_argument("--no-sandbox")
  24. options.add_argument("--headless")
  25. options.add_argument("--incognito")
  26. #options.add_argument('--blink-settings=imagesEnabled=false')
  27. mobile_emulation = {
  28. "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
  29. "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
  30. try:
  31. driver = webdriver.Chrome(options=options)
  32. # driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)
  33. ## Create Undetected Chromedriver with Options
  34. # driver = uc.Chrome(options=options)
  35. # options = uc.ChromeOptions()
  36. except:
  37. traceback.print_exc()
  38. #kw='真理大學國際生'
  39. #domain='cia.au.edu.tw'
  40. kw='真理大學校友'
  41. domain='au.edu.tw'
  42. driver.get('https://www.google.com?num=100')
  43. time.sleep(3)
  44. print(driver.current_url)
  45. elmt = driver.find_element(By.XPATH, "//textarea[@name='q']")
  46. time.sleep(1)
  47. elmt.send_keys(kw)
  48. elmt.send_keys(Keys.ENTER)
  49. time.sleep(4)
  50. elmts = driver.find_elements(By.XPATH, "//a[@jsname='UWckNb']")
  51. numresults=len(elmts)
  52. print('搜尋結果數量',numresults)
  53. if numresults<=0:
  54. print(elmts)
  55. for elmt in elmts:
  56. href=elmt.get_attribute('href')
  57. txt=elmt.text
  58. # print(txt)
  59. # print(href)
  60. if domain in href:
  61. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  62. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  63. print(domain)
  64. print(href)
  65. break
  66. time.sleep(5)