Python Firefox可以工作,但PhantomJS无法使用css选择器找到元素
为了提高速度,我最近从Python Firefox可以工作,但PhantomJS无法使用css选择器找到元素,python,python-3.x,selenium,datepicker,phantomjs,Python,Python 3.x,Selenium,Datepicker,Phantomjs,为了提高速度,我最近从webdriver.Firefox()改为webdriver.PhantomJS(),当我试图在我的日期选择器上查找一个元素以便在之后单击它时,我开始出现一些错误 self.driver = webdriver.PhantomJS() self.driver.set_window_size(1280, 1024) self.driver.find_element_by_css_selector( "#ui-datepi
webdriver.Firefox()
改为webdriver.PhantomJS()
,当我试图在我的日期选择器上查找一个元素以便在之后单击它时,我开始出现一些错误
self.driver = webdriver.PhantomJS()
self.driver.set_window_size(1280, 1024)
self.driver.find_element_by_css_selector(
"#ui-datepicker-div td.full-selected.full-changeover > a"
).click()
Message: {"errorMessage":"Unable to find element with css selector '#ui-datepicker-div td.full-selected.full-changeover > a'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"146","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:54784","User-Agent":"Python-urllib/3.5"},"httpVersion":"1.1","method":"POST","post":"{\"value\": \"#ui-datepicker-div td.full-selected.full-changeover > a\", \"sessionId\": \"8b584560-6eb6-11e6-bb4a-77906b62d5cb\", \"using\": \"css selector\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/8b584560-6eb6-11e6-bb4a-77906b62d5cb/element"}}
Screenshot: available via screen
我使用的是selenium==2.53.6
和phantomjs==2.1.12
更新(以下是代码):
有什么办法可以解决这个问题吗???我已经复制了您的问题,并能够通过最大化浏览器窗口来解决它:
是的,有了PhantomJS,你可能会有更多这样的情况,需要额外的等待、滚动到元素视图和其他“技巧”来支持。您是否尝试等待此元素出现?谢谢。嗨@alecxe。。如果我等待它,我会得到同样的错误:(…任何其他想法??很难说,你能发布完整的代码直到出现问题吗?谢谢。@alecxe是的,编辑我的问题并添加代码直到错误再次感谢@alecxe。我以前尝试过“最大化”窗口,但它不起作用(可能我还没有等待时间)
def get_price(self, url):
url = "https://www.homeaway.pt/arrendamento-ferias/p1823902"
# Lets reset it
self.driver.get(url)
wait = WebDriverWait(self.driver, 5)
prices = defaultdict(list)
count = 0
for month in range(self.month_count):
next_month_iteration = False
checkin_date = wait.until(
EC.visibility_of_element_located(
(
By.CSS_SELECTOR,
".quotebar-container input[id=startDateInput]"
)
)
)
checkin_date.click()
for counting in range(count):
try:
self.driver.execute_script(
'$( "a.ui-datepicker-next" ).click()'
)
except WebDriverException:
log.error(
'WebDriverException: Message: '
'getAvailabilityIndexForDate requires a Date object'
)
next_month_iteration = True
break
if next_month_iteration:
# Skip the next iteration cause there was an error
continue
year = self.driver.find_element_by_css_selector(
".ui-datepicker-year").text
current_month = self.driver.find_element_by_css_selector(
".ui-datepicker-month").text
log.info(
'Current Month is "%s"',
current_month.encode('ascii', 'ignore').decode('utf-8')
)
try:
first_available_checkin_date = wait.until(
EC.element_to_be_clickable(
(
By.CSS_SELECTOR,
"#ui-datepicker-div td.full-changeover > a"
)
)
)
except TimeoutException:
log.warning('Is there any date available here "%s" ?', url)
continue
else:
log.info(
'First_available_checkin_date is "%s"',
first_available_checkin_date.text
)
ActionChains(self.driver).move_to_element(
first_available_checkin_date).perform()
# self.driver.find_element_by_css_selector(
# "#ui-datepicker-div td.full-selected.full-changeover > a"
# ).click()
choose_date = wait.until(
EC.visibility_of_element_located(
(
By.CSS_SELECTOR,
"#ui-datepicker-div td.full-selected.full-changeover > a"
)
)
)
choose_date.click()
self.driver = webdriver.PhantomJS()
self.driver.maximize_window()