大佬教程收集整理的这篇文章主要介绍了Python Selenium 找不到 loadmore (mehr anzeigen) 按钮,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我基本上想在运行我的其余代码之前点击页面上的每个加载更多按钮,否则我将无法访问每个配置文件。
有两个问题:
首先我如何访问它?我尝试了与我的代码中的 FantasyCompLabel 部分类似的方法,但它不起作用。
第二个我不知道我应该如何遍历所有按钮,因为我认为第二个按钮只开始加载,直到第一个被点击。
这里是相关的HTML部分和按钮的图片
<span type="button" class="md-text-button button-orange-white" onclick="loadFollowing();">mehr anzeigen</span>
这是访问每个配置文件的代码,但正如您所看到的,它只运行到第一个加载按钮。
from bs4 import BeautifulSoup
import time
from SELEnium import webdriver
from SELEnium.webdriver.common.by import By
from SELEnium.webdriver.support import expected_conditions as EC
from SELEnium.webdriver.support.wait import webdriverwait
time.sleep(3)
# Set some SELEnium Options
options = webdriver.ChromeOptions()
# options.add_argument('--headless')
options.add_argument('--no-sandBox')
options.add_argument('--disable-dev-shm-usage')
# Webdriver
wd = webdriver.Chrome(executable_path='/usr/bin/Chromedriver',options=options)
# URL
url = 'https://www.techpilot.de/zulIEferer-suchen?laserschnEIDen%202d%20(laserstrahlschnEIDen)'
# Load URL
wd.get(url)
# Get HTML
soup = BeautifulSoup(wd.page_source,'HTMl.parser')
wait = webdriverwait(wd,15)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#bodyJsP #CybotcookiebotDialogBodyLevelbuttonLevelOpTinAllowAll"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"#efficIEntSearchiframe")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,".hidefunctionalScrollbar #CybotcookiebotDialogBodyLevelbuttonLevelOpTinAllowAll"))).click()
#wd.switch_to.default_content() # you do not need to switch to default content because iframe is closed already
wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,".fancyCompLabel")))
results = wd.find_elements_by_CSS_SELEctor(".fancyCompLabel")
for profil in results:
print(profil.text) #heres the rest of my code but its not relevant
wd.close()
正如我所见,位于 .hideFunctionalScrollbar #CybotCookiebotDialogBodyLevelButtonLevelOpTinAllowAll
的第二个弹出元素最初出现在可见屏幕之外。
因此,在切换到 iframe
后,您需要先滚动到该元素,然后再尝试单击它。
此外,presence_of_all_elements_located
实际上并不等待所有元素都存在。它甚至不知道有多少这样的元素。一旦找到至少 1 个与传递的定位符匹配的元素,它就会返回。
因此,我建议在该行之后添加一个短暂的睡眠,以允许实际加载所有这些元素。
from SELEnium.webdriver.common.action_chains import ActionChains
wait = WebDriverWait(wd,15)
actions = ActionChains(wd)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#bodyJSP #CybotCookiebotDialogBodyLevelButtonLevelOpTinAllowAll"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"#efficientSearchIframe")))
second_pop_up = wd.find_elemenT_By_css(".hideFunctionalScrollbar #CybotCookiebotDialogBodyLevelButtonLevelOpTinAllowAll")
actions.move_to_element(second_pop_up).build().perform()
time.sleep(0.5)
second_pop_up.click()
wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,".fancyCompLabel")))
time.sleep(0.5)
for profil in results:
print(profil.text) #heres the rest of my code but its not relevant
wd.close()
以上是大佬教程为你收集整理的Python Selenium 找不到 loadmore (mehr anzeigen) 按钮全部内容,希望文章能够帮你解决Python Selenium 找不到 loadmore (mehr anzeigen) 按钮所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。