Selenium WebDriver ile Google da aranılan kelimeden istenilen linki bulup ziyaret etme
Merhabalar;
Bu python kod parçaçığı ile google da arama yapar ve bulunan sonuçlarda 10 sayfa boyunca ilerler ve bulduğunda ise siteyi ziyaret eder.
author = 'rgpunal'
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
if __name__ == "__main__":
profile = webdriver.FirefoxProfile()
profile.native_events_enabled = True
driver = webdriver.Firefox(profile)
driver.maximize_window()
driver.implicitly_wait(15)
wait = WebDriverWait(driver, 10)
siteUrl = "ragipunal.com"
searchEngineUrl = "http://www.google.com"
maxPageNumber = 10
driver.get(searchEngineUrl)
inputElement = driver.find_element_by_name("q")
inputElement.send_keys("ragıp ünal" + Keys.RETURN)
pageNumber = 1
flag = False
while (not flag):
linkElements = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//h3[@class='r']/a")))
for toClick in linkElements:
action = ActionChains(driver)
action.move_to_element(toClick).perform()
sleep(1)
if (siteUrl in toClick.get_attribute("href")):
action.click()
flag = True
if not flag:
# Eğer max sayfa sayısını geçti ise döngüyü sonlandır.
if pageNumber > maxPageNumber:
break
try:
nextPage = wait.until(EC.element_to_be_clickable((By.XPATH,"//a[@id='pnnext']/span[1]")))
except:
break
sleep(2)
# Bu bölümde hareket kazandırıyoruz.
action = ActionChains(driver)
action.move_to_element(nextPage).perform()
sleep(5)
nextPage.click()
pageNumber += 1
pn = str(pageNumber)
wait.until(EC.text_to_be_present_in_element((By.XPATH, "//td[@class='cur']"), pn))
driver.quit()