如何让复选框点击使用 selenium ,Python

vngu2lb8  于 2023-06-04  发布在  Python
关注(0)|答案(3)|浏览(456)

无法使用python selenium,geckodriver单击复选框。
以下html中的输入行(来自inspect-element选项)显示为灰色,并且不突出显示复选框。

<div class="ui_checkbox BZGrt w">
 <input id="checkbox_590" type="checkbox" class="input" value="10697">
 <label class="label" for="checkbox_590" >
  ::before
  <div class="icWwC f u k w"><span class="PDJtN"><span class="mTKbF">Vegan Options</span></span></div></label></div>

当光标位于::before上时,复选框突出显示
我的Python代码如下:

import time
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

#link = link_list[0]
link = "https://www.tripadvisor.co.uk/Restaurants-g186338-London_England.html"
driver = webdriver.Firefox() 
time.sleep(2)
driver.get(link)
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler'))).click()
driver.find_elements(By.XPATH, '//*[@id="checkbox_590"]').click()

请帮助点击该复选框。

nle07wnf

nle07wnf1#

你可以通过文本找到,这不是最好的方法,但它工作正常。

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Set up driver
driver = webdriver.Firefox()
driver.get("https://www.tripadvisor.co.uk/Restaurants-g186338-London_England.html")

# Wait for the cookie consent button to be clickable and click it
cookie_consent_button = WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.ID, 'onetrust-accept-btn-handler')))
cookie_consent_button.click()

# Generate GUI path
gui_path = '//span[text()="Vegetarian Friendly"]'

# Find and click on "Vegetarian Friendly"
element = driver.find_element(By.XPATH, gui_path)
element.click()

# Close the browser
driver.quit()
o4hqfura

o4hqfura2#

你可以通过标签找到,它不是最好的,但我尝试它的工作。

import time
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
from selenium.webdriver.common.action_chains import ActionChains

#link = link_list[0]
link = "https://www.tripadvisor.co.uk/Restaurants-g186338-London_England.html"
driver = webdriver.Firefox() 
time.sleep(2)
driver.get(link)
driver.implicitly_wait(10)
driver.find_element(By.XPATH,"//label[@for='checkbox_590']").click()
ttp71kqs

ttp71kqs3#

要单击与<label>关联的checkboxVegan Options,您需要为element_to_be_clickable()诱导WebDriverWait,您可以使用以下locator strategy

  • 使用 XPATH
driver.get("https://www.tripadvisor.co.uk/Restaurants-g186338-London_England.html")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Vegan Options"]//ancestor::label[1]'))).click()

*注意:需要添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
  • 浏览器快照:

相关问题