如何使用Selenium修复python中的日期选择器

2024-09-29 21:47:35 发布

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

我正在尝试用python和selenium制作一个自动注册机器人。我要做的事情最多,因为它们不那么难。但自动取款机我被一个约会者困住了。代码可以打开日期框,但不能选择日期。另一个问题是,你不能在日期框中写任何东西,你必须在日期框中选择一个日期。在

我尝试了在stackoverflow上找到的各种方法,但是这个网站没有任何效果。在

站点:https://mobilepanel2.nielsen.com/enrol/home?l=de_de&pid=9

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
b = webdriver.Chrome(r'''C:\Users\Florian\PycharmProjects\Auto_Reg\chromedriver''')
b.get('https://mobilepanel2.nielsen.com/enrol/home?l=de_de&pid=9')
b.find_element_by_xpath("//select[@id='platform']/option[contains(text(),'Android')]").click()
b.find_element_by_xpath("//select[@id='deviceType']/option[contains(text(),'Smartphone')]").click()
b.find_element_by_xpath("//label[contains(text(),'Männlich')]").click()
## until here, everything works fine 

select = Select(b.find_element_by_name('birthDate'))
select.select_by_visible_text("13")

Tags: textfromhttpsimportbyseleniumdeelement
1条回答
网友
1楼 · 发布于 2024-09-29 21:47:35

给你:

# click calendar to appear
browser.find_element_by_id('birthDateCalendar').click()

# get calendar elements
calendar = browser.find_elements_by_xpath('//*[@id="ui-datepicker-div"]/table/tbody/tr/td')

# click selected day
selection = '15'
for item in calendar:
    day = item.get_attribute("innerText")
    if day == selection:
        item.click()

相关问题 更多 >

    热门问题