无法单击加载更多按钮,其中包含 href = 使用 python 的 javascript void



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

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()

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

元素点击拦截异常: 消息: 元素点击拦截: 元素...在点 (455, 863( 处不可单击。其他元素将

然后我尝试使用以下代码

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 = driver.page_source.encode('utf-8'( 上面的代码给了我另一个错误 元素点击拦截异常: 消息: 元素点击拦截: 元素...在点 (455, 863( 处不可单击。其他元素将收到点击:... (会话信息: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()

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

我不确定我哪里出错了,任何人都可以帮助我如何单击"加载更多"按钮并加载所有页面,以便我可以从页面中提取所有 href 元素。

观察到接受cookie的通知正在中断点击,并且您没有等待加载元素。

有不同的方法,但我使用了 1 个 Web驱动程序等待 . 检查以下行是否适合您...

更改驱动程序路径

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

最新更新