在脚本元素中查找并单击元素

2024-04-26 22:56:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python和selenium更改网站上的商店位置。 Firefox的Selenium IDE插件给了我一个序列。这个序列可以在Firefox的selenium IDE中工作,但是我无法在Python(Spyder)中工作。我要单击的元素在脚本中,没有工具可以在脚本中找到元素。美苏做不到,硒也做不到。 使用下面的代码,我试图得到每个商店的产品价格,所以我需要将商店(它是左上角的黄色按钮,然后是下拉列表)从下拉列表中更改为每个商店,并取消产品价格的页面源代码。但每当我尝试“driver.find\ element\ by\时,我会得到“Unable to locate element:”

点击顺序是用Firefox中的seleniumide插件记录的。 或者也许有一种比硒更快的方法在商店之间切换并获得产品价格。我不能光靠美女来做

from selenium import webdriver
driver = webdriver.Firefox(executable_path='d:\Work\geckodriver.exe')
url = 'https://www.castorama.pl/deska-14x90x540-eslov-jodel-1-94-id-1105153.html'
driver.get(url)
driver.maximize_window()
driver.find_element_by_id("market-name").click() #Unable to locate element
driver.find_element_by_id("shop-selection-master-infostore").click()
driver.find_element_by_xpath("//div[@id='geolocation_popup_select_market_chosen']/a/span").click()
driver.find_element_by_xpath("//div[@id='geolocation_popup_select_market_chosen']/div/ul/li[2]").click()

Tags: div插件idbydriverselenium序列element
1条回答
网友
1楼 · 发布于 2024-04-26 22:56:24

关闭cookie bar:

from selenium import webdriver

driver = webdriver.Firefox(executable_path='d:\Work\geckodriver.exe')
url = 'https://www.castorama.pl/deska-14x90x540-eslov-jodel-1-94-id-1105153.html'
driver.get(url)
driver.maximize_window()
driver.find_element_by_css_selector('[onclick="bold.cookie.closeCookie();"]').click()
driver.find_element_by_id('shop-selection-master').click()

纯粹主义者不会喜欢它,但你也可以使用javascript

driver.execute_script("document.querySelector('#shop-selection-master').click();")

这里详细介绍了选择选项:Selenium - Python - drop-down menu option value

您拥有父级的id selectgeolocation-popup-select-market

相关问题 更多 >