Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用BeautifulSoup进行网页抓取,单击隐藏选项卡的元素_Python_Beautifulsoup_Web Crawler - Fatal编程技术网

Python 使用BeautifulSoup进行网页抓取,单击隐藏选项卡的元素

Python 使用BeautifulSoup进行网页抓取,单击隐藏选项卡的元素,python,beautifulsoup,web-crawler,Python,Beautifulsoup,Web Crawler,我在试图捕获页面内的特定信息时遇到问题 网站: 在此页面上,在“关于此项目”下的“详细信息”选项卡旁边有一些隐藏选项卡,名为“标签信息”、“发货和退货”、“Q&A”,我想清除这些选项卡 我发现我需要在使用Beautifulsoup进行刮取之前单击这些元素 这是我的代码,假设每个链接都有pid url = 'https://www.target.com' + str(pid) driver.get(url) driver.implicitly_wait(5)

我在试图捕获页面内的特定信息时遇到问题

网站:

在此页面上,在“关于此项目”下的“详细信息”选项卡旁边有一些隐藏选项卡,名为“标签信息”、“发货和退货”、“Q&A”,我想清除这些选项卡

我发现我需要在使用Beautifulsoup进行刮取之前单击这些元素

这是我的代码,假设每个链接都有pid


    url = 'https://www.target.com' + str(pid)
    
    driver.get(url)
    driver.implicitly_wait(5)
    
    soup = bs(driver.page_source, "html.parser")
    wait = WebDriverWait(driver, 3)
    
    button = soup.find_all('li', attrs={'class': "TabHeader__StyledLI-sc-25s16a-0 jMvtGI"})
    
    index = button.index('tab-ShippingReturns')
    print('The index of ShippingReturns is:', index)
    
    if search(button, 'tab-ShippingReturns'):
       button_shipping_returns = button[index].find_element_by_id("tab-ShippingReturns")
       button_shipping_returns.click()
    time.sleep(3)

我的代码返回 ResultSet对象没有属性“按\u id查找\u元素”。您可能将元素列表视为单个元素。当您打算调用find()时,是否调用了find_all()

有谁能告诉我如何解决这个问题吗?

下面是

button = soup.find_all('li', attrs={'class': "TabHeader__StyledLI-sc-25s16a-0 jMvtGI"})
将返回一个BeautifulSoup标记列表

然后尝试使用以下命令调用该列表上的selenium方法:

button_shipping_returns = button[index].find_element_by_id("tab-ShippingReturns")
相反,您需要在webdriver元素集合中调用它

driver.find_elements_by_css_selector('.TabHeader__StyledLI-sc-25s16a-0 jMvtGI')[index].find_element_by_id("tab-ShippingReturns")

似乎您尝试交互的按钮通过在末尾添加唯一值来动态生成类,我建议使用xpath选择器类型的contains()方法,如:

driver.find_elements_by_xpath("//a[contains(@class,'TabHeader__Styled')]")
因此,您的代码应该如下所示:

   elements = driver.find_elements_by_xpath("//a[contains(@class,'TabHeader__Styled')]")
   for el in elements:
       el.click()
点击页面所在的按钮并不可怕

如果元素不可见且需要向下滚动,则可以使用ActionChains:

from selenium.webdriver import ActionChains
ActionChains(driver).move_to_element(el).perform()
代码如下所示,只是您的元素解析器:

from selenium import webdriver
from selenium.webdriver import ActionChains

driver = webdriver.Chrome()
url = 'https://www.target.com/p/prairie-farms-vitamin-d-milk-1gal/-/A-47103206#lnk=sametab'
driver.get(url)
driver.implicitly_wait(5)

elements = driver.find_elements_by_xpath("//a[contains(@class,'TabHeader__Styled')]")
for el in elements:
    ActionChains(driver).move_to_element(el).perform()
    el.click()

driver.quit()

这是“刮”而不是“刮”。谢谢,QHarr,我发现它仍然有一条错误消息,但我用下面Vova的代码解决了这个问题。谢谢你的帮助和支持。