Ladislaff
Ladislaff

Reputation: 21

Python, Selenium. Google Chrome. Web Scraping. How to navigate between 'tabs' in website

im quite noob in python and right now building up a web scraper in Selenium that would take all URL's for products in the clicked 'tab' on web page. But my code take the URL's from the first 'tab'. Code below. Thank you guys. Im starting to be kind of frustrated lol. Screenshot

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from lxml import html 


PATH = 'C:\Program Files (x86)\chromedriver.exe'

driver = webdriver.Chrome(PATH)

url = 'https://www.alza.sk/vypredaj-akcia-zlava/e0.htm'

driver.get(url)

driver.find_element_by_xpath('//*[@id="tabs"]/ul/li[2]').click()

links = []

try:
    WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, 'blockFilter')))
    link = driver.find_elements_by_xpath("//a[@class='name browsinglink impression-binded']")
    
    for i in link:
        links.append(i.get_attribute('href'))

finally:
    driver.quit()

print(links)

Upvotes: 2

Views: 728

Answers (1)

Renan Lopes
Renan Lopes

Reputation: 411

To select current tab:

current_tab = driver.current_window_handle

To switch between tabs:

driver.switch_to_window(driver.window_handles[1])
driver.switch_to.window(driver.window_handles[-1])

Assuming you have the new tab url as TAB_URL, you should try:

from selenium.webdriver.common.action_chains import ActionChains
action = ActionChains(driver)
action.key_down(Keys.CONTROL).click(TAB_URL).key_up(Keys.CONTROL).perform() 

Also, apparently the li doesn't have a click event, are you sure this element you are getting '//*[@id="tabs"]/ul/li[2]' has the aria-selected property set to true or any of these classes: ui-tabs-active ui-state-active?

If not, you should call click on the a tag inside this li.

Then you should increase the timeout parameter of your WebDriverWait to guarantee that the div is loaded.

Upvotes: 3

Related Questions