简体   繁体   English

Python,Selenium。 谷歌浏览器。 Web 报废。 如何在网站的“标签”之间导航

[英]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. 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'.但是我的代码从第一个“标签”中获取 URL。 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)

To select current tab:至 select 当前标签:

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:假设您有新标签 url 作为TAB_URL ,您应该尝试:

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 ?另外,显然li没有点击事件,你确定你得到的这个元素'//*[@id="tabs"]/ul/li[2]'aria-selected属性设置为true或任何这些类: ui-tabs-active ui-state-active

If not, you should call click on the a tag inside this li .如果没有,您应该调用单击li内的a标签。

Then you should increase the timeout parameter of your WebDriverWait to guarantee that the div is loaded.然后你应该增加你的WebDriverWaittimeout参数来保证div被加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM