英文:
Airbnb currency display button element uninteractable using Selenium in Python
问题
I'm here to help with your translation request. Here's the translated text:
我是Python和Selenium的新手,尤其对HTML一无所知。我正在使用Selenium来爬取Airbnb的房源和价格,仅仅是为了增加价格筛选器,以便我可以在不受15页限制的情况下访问更多地区的房源。
我一直在尝试弄清楚如何与Airbnb结果页面上的货币显示按钮进行交互,并选择显示美元(目前在夏季在国外,我的连接速度太慢,无法使用代理运行程序)。
我尝试了代理,但没有免费可用的代理,而且使用时间不稳定。我还尝试添加了有关货币和国家的Cookie,但不断收到“InvalidCookieDomainException”错误,没有解决此问题的方法适用于我。因此,我决定手动更改页面底部的货币。
供参考,这是按钮元素:
<button type="button" class="_1az8yk66 l1ovpqvx dir dir-ltr"><span class="a8jt5op dir dir-ltr">选择货币</span><span class="_1vel3z8">$</span><span class="_144l3kj">SGD</span></button>
我原始代码中还有其他部分,可以成功点击页面顶部的其他按钮并滚动到不同的元素,但是这个货币按钮特别无法使用,我完全被难住了。
我的第一次尝试是等待按钮可点击,以下是代码片段:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.maximize_window()
link = 'https://www.airbnb.com/s/Pike-County--PA--United-States/homes?tab_id=home_tab&refinement_paths%5B%5D=%2Fhomes&monthly_start_date=2023-07-01&monthly_length=3&price_filter_input_type=0&price_filter_num_nights=5&channel=EXPLORE&date_picker_type=flexible_dates&flexible_trip_dates%5B%5D=april&flexible_trip_dates%5B%5D=august&flexible_trip_dates%5B%5D=december&flexible_trip_dates%5B%5D=february&flexible_trip_dates%5B%5D=january&flexible_trip_dates%5B%5D=july&flexible_trip_dates%5B%5D=june&flexible_trip_dates%5B%5D=march&flexible_trip_dates%5B%5D=may&flexible_trip_dates%5B%5D=november&flexible_trip_dates%5B%5D=october&flexible_trip_dates%5B%5D=september&adults=3&source=structured_search_input_header&search_type=filter_change&query=Pike%20County%2C%20PA&zoom_level=11&place_id=ChIJXQMzOISixIkRNmmWbNFqb2w&flexible_trip_lengths%5B%5D=one_week'
driver.get(link)
currency_element = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='_1az8yk66 l1ovpqvx dir dir-ltr']")))
currency_element.click()
然而,这只导致了超时,指的是等待元素可点击。我认为这意味着元素不可点击,所以我尝试在不等待可点击的情况下访问该按钮。
currency_element = wait.until(EC.presence_of_element_located((By.XPATH, "//button[@class='_1az8yk66 l1ovpqvx dir dir-ltr']")))
currency_element.click()
这给了我一个错误:selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Selenium可以定位元素,但似乎无法滚动或点击它。我不知道为什么,因为它是一个按钮?
我尝试了多种类似问题的解决方案:
英文:
I'm new to Python and Selenium, but especially know nothing about HTML. I'm using Selenium to scrape airbnb listings and their prices solely to increment the price filter so I can access more listings per area without hitting the 15 page cap.
I've been trying to figure out how to interact with the currency display button on an airbnb results page and choose to display USD (currently abroad for the summer and my connection is far too slow to run the program with a proxy).
I tried proxies but none free and available were usable for hours (inconsistent).
I also tried adding cookies for currency and country but kept getting 'InvalidCookieDomainException' no solutions posted on this issue worked for me.
So I decided to change the currency manually with the currency button on the bottom of the page.
For reference, this is the button element:
<button type="button" class="_1az8yk66 l1ovpqvx dir dir-ltr"><span class="a8jt5op dir dir-ltr">Choose a currency</span><span class="_1vel3z8">$</span><span class="_144l3kj">SGD</span></button>
I have other parts in my original code that clicks other buttons on the top of the page and scrolls to different elements successfully, but this currency button specifically does not work and I'm completely stumped.
My first attempt was to wait for the button to be clickable, here is a snippet of the code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.maximize_window()
link = 'https://www.airbnb.com/s/Pike-County--PA--United-States/homes?tab_id=home_tab&refinement_paths%5B%5D=%2Fhomes&monthly_start_date=2023-07-01&monthly_length=3&price_filter_input_type=0&price_filter_num_nights=5&channel=EXPLORE&date_picker_type=flexible_dates&flexible_trip_dates%5B%5D=april&flexible_trip_dates%5B%5D=august&flexible_trip_dates%5B%5D=december&flexible_trip_dates%5B%5D=february&flexible_trip_dates%5B%5D=january&flexible_trip_dates%5B%5D=july&flexible_trip_dates%5B%5D=june&flexible_trip_dates%5B%5D=march&flexible_trip_dates%5B%5D=may&flexible_trip_dates%5B%5D=november&flexible_trip_dates%5B%5D=october&flexible_trip_dates%5B%5D=september&adults=3&source=structured_search_input_header&search_type=filter_change&query=Pike%20County%2C%20PA&zoom_level=11&place_id=ChIJXQMzOISixIkRNmmWbNFqb2w&flexible_trip_lengths%5B%5D=one_week'
driver.get(link)
currency_element = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@class='_1az8yk66 l1ovpqvx dir dir-ltr']")))
currency_element.click()
This however, only resulted in a timeout, referring to the wait for element to be clickable. I assumed this meant that the element was not clickable and so I tried to access the button without waiting for it to be clickable.
currency_element = wait.until(EC.presence_of_element_located((By.XPATH, "//button[@class='_1az8yk66 l1ovpqvx dir dir-ltr']")))
currency_element.click()
This gave me an error of: selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Selenium manages to locate the element but somehow cannot scroll to it or click it. I'm not sure why as it is a button?
I tried multiple solutions from similar issues posted.
- using ActionChains to move/scroll to the element and then click.
- never actually scrolls to the bottom of the page or to where the element is located - not sure why. always returns element not interactable errors.
- checking for iframes (none blocking)
- the only iframes on the page were nowhere near the bottom. though, I'm not actually sure how to navigate this.
- using CSS selector to access the element
- no difference in outcome
I also tried adding implicit waits in case any of the issues were due to connectivity and loading issues but still same errors for all. I'm not sure what exactly it is with the airbnb page that limits
Any help, ideas, or just a general push to a different direction will be appreciated. Thanks in advance.
答案1
得分: 0
以下是代码中需要翻译的部分:
Refer the below working code: This code will launch URL --> Click on OK preference button --> Selects USD as currency
import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.airbnb.co.uk/s/Pike-County--PA--United-States/homes?tab_id=home_tab&refinement_paths%5B%5D=%2Fhomes&monthly_start_date=2023-08-01&monthly_length=3&price_filter_input_type=0&price_filter_num_nights=5&channel=EXPLORE&date_picker_type=flexible_dates&flexible_trip_dates%5B%5D=april&flexible_trip_dates%5B%5D=august&flexible_trip_dates%5B%5D=december&flexible_trip_dates%5B%5D=february&flexible_trip_dates%5B%5D=january&flexible_trip_dates%5B%5D=july&flexible_trip_dates%5B%5D=june&flexible_trip_dates%5B%5D=march&flexible_trip_dates%5B%5D=may&flexible_trip_dates%5B%5D=november&flexible_trip_dates%5B%5D=october&flexible_trip_dates%5B%5D=september&adults=3&source=structured_search_input_header&search_type=filter_change&query=Pike%20County%2C%20PA&zoom_level=11&place_id=ChIJXQMzOISixIkRNmmWbNFqb2w&flexible_trip_lengths%5B%5D=one_week&federated_search_session_id=3246c11b-bcbe-4098-a744-ef0846c4e420&pagination_search=true&cursor=eyJzZWN0aW9uX29mZnNldCI6MywiaXRlbXNfb2Zmc2V0IjoyNTIsInZlcnNpb24iOjF9&_set_bev_on_new_domain=1688479971_YWJmNzlmOTM1M2Iw")
driver.maximize_window()
wait = WebDriverWait(driver, 30)
# Click OK button on preference
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='OK']"))).click()
# Below 3 lines of code will click on Globe icon, Currency tab and USD
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Choose a language and currency']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Currency']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='United States dollar']"))).click()
time.sleep(10)
请注意,我已经去除了HTML实体编码(如"
)并将文本翻译为中文。
英文:
Refer the below working code: This code will launch URL --> Click on OK preference button --> Selects USD as currency
import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.airbnb.co.uk/s/Pike-County--PA--United-States/homes?tab_id=home_tab&refinement_paths%5B%5D=%2Fhomes&monthly_start_date=2023-08-01&monthly_length=3&price_filter_input_type=0&price_filter_num_nights=5&channel=EXPLORE&date_picker_type=flexible_dates&flexible_trip_dates%5B%5D=april&flexible_trip_dates%5B%5D=august&flexible_trip_dates%5B%5D=december&flexible_trip_dates%5B%5D=february&flexible_trip_dates%5B%5D=january&flexible_trip_dates%5B%5D=july&flexible_trip_dates%5B%5D=june&flexible_trip_dates%5B%5D=march&flexible_trip_dates%5B%5D=may&flexible_trip_dates%5B%5D=november&flexible_trip_dates%5B%5D=october&flexible_trip_dates%5B%5D=september&adults=3&source=structured_search_input_header&search_type=filter_change&query=Pike%20County%2C%20PA&zoom_level=11&place_id=ChIJXQMzOISixIkRNmmWbNFqb2w&flexible_trip_lengths%5B%5D=one_week&federated_search_session_id=3246c11b-bcbe-4098-a744-ef0846c4e420&pagination_search=true&cursor=eyJzZWN0aW9uX29mZnNldCI6MywiaXRlbXNfb2Zmc2V0IjoyNTIsInZlcnNpb24iOjF9&_set_bev_on_new_domain=1688479971_YWJmNzlmOTM1M2Iw")
driver.maximize_window()
wait = WebDriverWait(driver, 30)
# Click OK button on preference
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='OK']"))).click()
# Below 3 lines of code will click on Globe icon, Currency tab and USD
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Choose a language and currency']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Currency']"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='United States dollar']"))).click()
time.sleep(10)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论