无法处理一些关键布局的链接 [英] Unable to deal with some critically laid out links

查看:67
本文介绍了无法处理一些关键布局的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用python与硒结合编写了一个脚本,以解析网页中的某些信息.该网页上的商店名称及其链接很少.如果单击任何链接,将弹出包含信息的卡片(如盒子).我的意图是单击每个链接并解析该卡中的信息.但是,当我执行脚本时,它会引发错误,显示timeout exception.我什至将浏览器向下滚动到元素所在的位置,以确保是否由于时间原因无法单击任何链接.但是,它仍然引发相同的错误.我该如何解决?预先感谢.

I've written a script in python in combination with selenium to parse some information from a webpage. There are few store names along with their links on that webpage. If I click on any of the links, a card like a box pops up containing information. My intention is to click on each link and parse information from that card. However, when I execute my script, it throws an error showing timeout exception. I even scrolled down my browser to that position where elements are located to be sure whether it is because of time that it can't click on any link. However, it still throws the same error. How can I get around that? Thanks in advance.

这是该网页的链接:页面链接

这是我的尝试:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get("above_link")

for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "#addresses_list li a"))):
    item.click
driver.quit()

链接所在的元素:

<li style="list-style:none; background-image:url(&quot;https://storelocator.w3apps.co/images/orange.png&quot;); background-repeat:no-repeat; background-position:0px 10px; padding:10px; padding-left:35px; border-bottom:1px dashed #ddd;" onmouseover="hoverStart(601538)" onmouseout="hoverStop(601538)"><a href="#" onclick="focus_and_popup(601538); return false;"><div class="gm_add_name">Adel Outfitters</div>1221 W 4th St <br>Adel Georgia 31620<br><div style="display:none" class="w3-address-country">United States</div>229-896-7105</a><div class="gm_add_distance"></div><a target="_blank" class="directions-link" href="http://maps.google.com/?saddr=+&amp;daddr=1221+W 4th St, Adel, Georgia, 31620">Directions<span class="w3-arrow">➲</span></a></li>

链接位于地图的左侧区域.

The links are located in the left-side area of the map.

推荐答案

正如@Grasshopper正确建议的那样,您需要切换到iframe才能处理链接.试试

As @Grasshopper correctly suggested you need to switch to iframe to be able to handle links. Try

driver.switch_to.frame("store-locator-iframe")
for item in wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "#addresses_list li a"))):
    driver.execute_script('arguments[0].scrollIntoView();', item)
    item.click()
    ...

# Switch back if you need to handle elements outside of iframe
driver.switch_to.default_content()

这篇关于无法处理一些关键布局的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆