使用python selenium headless时出现超时错误
如果不使用selenium headless,以下代码可以正常工作。 但为什么我在使用无头模式时出错。 这是我的密码:-使用python selenium headless时出现超时错误,python,python-3.x,selenium,selenium-webdriver,web-scraping,Python,Python 3.x,Selenium,Selenium Webdriver,Web Scraping,如果不使用selenium headless,以下代码可以正常工作。 但为什么我在使用无头模式时出错。 这是我的密码:- from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait fr
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_argument("--disable-notifications")
options.headless = True
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)
url = "https://www.justdial.com/Delhi/S-K-Premium-Par-Hari-Nagar/011PXX11-XX11-131128122154-B8G6_BZDET"
driver.get(url)
pop_up = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="best_deal_detail_div"]/section/span')))
time.sleep(2.5)
pop_up.click() # For disable pop-up
while True:
try:
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//span[text()='Load More Reviews..']")))
element.click()
except TimeoutException:
break
except:
pass
print([span.text for span in driver.find_elements_by_css_selector('span.rName.lng_commn')])
请给出一些建议或解决方案。谢谢。错误的根本原因很清楚。这是由于脚本无法定位元素导致的,该元素由
显式等待持有
如果您尝试了评论中提到的答案,那么这也可能与无头
和无头
模式下的异常行为问题有关。在某些情况下,无头模式无法通过元素的id
检测元素,在这种情况下,元素通过DOM
的完整绝对路径
例如,在下面的示例中,传递完整xpath
,看看它是否解决了问题
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, <full xpath>)))
element.click()
element=WebDriverWait(驱动程序,20)。直到(
可点击的元素((By.XPATH,))
元素。单击()
这是否回答了您的问题@Karthik不,我试过这些答案,问题解决了吗?