[英]Getting tImeout error when I am using python selenium headless
Without using the selenium headless the following code works fine.如果不使用无头硒,以下代码可以正常工作。 But Why I am getting error when I am using headless mode.
但是为什么我在使用无头模式时会出错。 Here is my code:-
这是我的代码:-
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')])
Kindly give some suggestions or solution.请提供一些建议或解决方案。 Thank you.
谢谢你。
The root cause of the error is clear.错误的根本原因是明确的。 It's happening as a result of the script not being able to locate the element -that is held by an
explicit-wait
.这是由于脚本无法定位元素 - 即由
explicit-wait
持有的元素而发生explicit-wait
。
Given that you tried the answers mentioned in the comments then this could also be to do with the anomaly behaviour issue in non-headless
and headless
mode.鉴于您尝试了评论中提到的答案,那么这也可能与
non-headless
和headless
模式下的异常行为问题有关。 There are situations in which a headless mode cannot detect elements by their id
in which case pass in the full absolute-path
of the DOM
.在某些情况下,无头模式无法通过
id
检测元素,在这种情况下,传入DOM
的完整absolute-path
。
Eg In the following, pass full xpath
and see if it solves the problem.例如,在下面,传递
full xpath
并查看它是否解决了问题。
element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, <full xpath>)))
element.click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.