无法使用python单击包含href=javascript void的load more按钮

2024-10-02 18:24:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从网页“https://careers.wipro.com/search-for-jobs.aspx?div=Technologies&#divhere”提取所有URL数据。此网站包含一个“加载更多”按钮,可加载更多页面。 我尝试了下面的代码,通过单击“加载更多”按钮,首先加载所有页面

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

driver = webdriver.Chrome()
driver.get("https://careers.wipro.com/search-for-jobs.aspx?div=Technologies&#divhere")

main_menu = driver.find_element_by_css_selector('#DivVeiwMore .button-show-more').click()

actions = ActionChains(driver)
actions.move_to_element(main_menu).click().build().perform()

上面的代码给出了以下错误

元素ClickInterceptedException:消息:元素ClickIntercepted:元素。。。在点(455863)处不可单击。其他因素会

然后我尝试使用下面的代码

url = "https://careers.wipro.com/search-for-jobs.aspx? 
div=Technologies&#divhere"
driver = webdriver.Chrome()
driver.get(url)
html = driver.page_source.encode('utf-8')
page_num = 0

while driver.find_elements_by_css_selector('#DivVeiwMore .button-show-more'):
element = driver.find_element_by_css_selector('#DivVeiwMore .button-show-more').click()
driver.execute_script("arguments[0].click();", element)
page_num += 1
print("getting page number" +str(page_num))
time.sleep(10)

html=驱动程序.page\u源代码('utf-8') 上面的代码给了我另一个错误 元素ClickInterceptedException:消息:元素ClickIntercepted:元素。。。在点(455863)处不可单击。其他元素将收到单击:。。。 (会话信息:chrome=81.0.4044.138)

然后我尝试使用wait,但效果不好。请在下面查找代码和错误

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

driver = webdriver.Chrome()
driver.get("https://careers.wipro.com/search-for-jobs.aspx? 
div=Technologies&#divhere")

main_menu = 
driver.find_element_by_css_selector('a[href="javascript:void(0);"]').click()

#actions = ActionChains(driver)
#actions.move_to_element(main_menu).click().build().perform()

wait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 
'a[href="javascript:void(0);"]'))).click()

这给了我一个错误 ElementNotInteractiableException:消息:元素不可交互

我不知道我哪里出了问题,谁能帮我点击加载更多按钮并加载所有页面,这样我就可以从页面中提取所有href元素


Tags: 代码fromhttpsimportcom元素bydriver
1条回答
网友
1楼 · 发布于 2024-10-02 18:24:14

所观察到的是,接受cookie的通知中断了单击,并且您没有等待加载元素

有不同的方法,但我使用了1个Webdriverwait。 检查以下行是否适合您

更改驱动程序路径

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

opt=webdriver.ChromeOptions()

opt.add_argument(" start-maximized")

driver= webdriver.Chrome(executable_path="C:\\chrome driver\\chromedriver.exe",options=opt)
driver.get("https://careers.wipro.com/search-for-jobs.aspx?div=Technologies&#divhere")

# if Accept cookie and decline cookie notification is visible in 5 sec then click on decline 
try:
    WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="DivCookie"]/div/div/div/a[2]/button')))
    driver.find_element_by_xpath('//*[@id="DivCookie"]/div/div/div/a[2]/button').click()
except TimeoutException:
    pass

WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#DivVeiwMore .button-show-more')))
time.sleep(1) # just to ensure element is properly loaded
main_menu = driver.find_element_by_css_selector('#DivVeiwMore .button-show-more').click()
# you can use Action chain as well

相关问题 更多 >