gpt4 book ai didi

Python Selenium : Element is not currently visible and may not be manipulated

转载 作者:行者123 更新时间:2023-12-01 09:17:27 28 4
gpt4 key购买 nike

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
import time

driver = webdriver.Chrome()
driver.get('https://www.bungol.ca/')
driver.set_window_size(width=1300, height=750)
time.sleep(1)

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, './/button[@type="submit" and text()="Search"]'))).click()

wait.until(EC.element_to_be_clickable((By.ID, 'activeListings'))).click() #sold
wait.until(EC.element_to_be_clickable((By.ID, 'useDateRange'))).click()

# I found that I had to click the start date every time I wanted to interact with
# anything related to the date selection div/table

#START DATE CALENDAR SELECT

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeStart" and @name="soldDateStart"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeStart" and @name="soldDateStart"]').click()
start_year = Select(driver.find_element_by_xpath('.//select[@class="pika-select pika-select-year"]'))
start_year.select_by_visible_text('2015')

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeStart" and @name="soldDateStart"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeStart" and @name="soldDateStart"]').click()
start_month = Select(driver.find_element_by_css_selector(".pika-select-month"))
start_month.select_by_visible_text("January")

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeStart" and @name="soldDateStart"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeStart" and @name="soldDateStart"]').click()
driver.find_element_by_xpath('.//td[@data-day="1"]').click()

time.sleep(2)

#END DATE CALENDAR SELECT

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeEnd" and @name="soldDateEnd"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeEnd" and @name="soldDateEnd"]').click()
end_year = Select(driver.find_element_by_css_selector(".pika-select-year"))
end_year.select_by_visible_text('2015')

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeEnd" and @name="soldDateEnd"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeEnd" and @name="soldDateEnd"]').click()
end_month = Select(driver.find_element_by_css_selector(".pika-select-month"))
end_month.select_by_visible_text('December')

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeEnd" and @name="soldDateEnd"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeEnd" and @name="soldDateEnd"]').click()
driver.find_element_by_xpath('.//td[@data-day="31"]').click()

网站引用:https://www.bungol.ca/map/

我在点击侧栏上的结束日期时遇到问题。奇怪的是,如果我通过先选择结束日期然后选择开始日期来运行代码,则会选择结束日期但不会选择开始日期。所以我只能在同一个脚本中选择一个日期。

当我尝试选择两个日期时,出现错误:

selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见:元素当前不可见,并且可能无法操作( session 信息:chrome=67.0.3396.99)(驱动程序信息:chromedriver=2.40.565386(45a059dc425e08165f9a10324bd1380cc13ca363),platform=Mac OS X 10.13.5 x86_64)

正如你所看到的,我确实尝试过延迟。

最佳答案

你的一些地方的 xPath 是错误的,我已经全部修复了:

driver.get('https://www.bungol.ca/')
driver.set_window_size(width=1300, height=750)
time.sleep(1)

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, './/button[@type="submit" and text()="Search"]'))).click()

wait.until(EC.element_to_be_clickable((By.ID, 'activeListings'))).click() #sold
wait.until(EC.element_to_be_clickable((By.ID, 'useDateRange'))).click()

# I found that I had to click the start date every time I wanted to interact with
# anything related to the date selection div/table

#START DATE CALENDAR SELECT

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeStart" and @name="soldDateStart"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeStart" and @name="soldDateStart"]').click()
start_year = Select(driver.find_element_by_xpath('.//select[@class="pika-select pika-select-year"]'))
start_year.select_by_visible_text('2015')

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeStart" and @name="soldDateStart"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeStart" and @name="soldDateStart"]').click()
start_month = Select(driver.find_element_by_css_selector(".pika-select-month"))
start_month.select_by_visible_text("January")

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeStart" and @name="soldDateStart"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeStart" and @name="soldDateStart"]').click()
driver.find_element_by_xpath('.//td[@data-day="1"]').click()

time.sleep(2)

#END DATE CALENDAR SELECT

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeEnd" and @name="soldDateEnd"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeEnd" and @name="soldDateEnd"]').click()
end_year = Select(driver.find_elements_by_css_selector(".pika-select-year")[1]) # here
end_year.select_by_visible_text('2015')

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeEnd" and @name="soldDateEnd"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeEnd" and @name="soldDateEnd"]').click()
end_month = Select(driver.find_elements_by_css_selector(".pika-select-month")[1]) # here
end_month.select_by_visible_text("December")

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeEnd" and @name="soldDateEnd"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeEnd" and @name="soldDateEnd"]').click()
driver.find_elements_by_xpath('.//td[@data-day="31"]')[1].click() # here

print("END")

问题是您对两个日历使用了相同的选择器。这意味着当您尝试与第二个日历交互时,您实际上仍在与第一个日历交互。 driver.find_elements[1] 解决了问题

关于Python Selenium : Element is not currently visible and may not be manipulated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51112312/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com