python-3.x 如何用xpath选择复选框元素?

3lxsmp7m  于 2023-03-04  发布在  Python
关注(0)|答案(1)|浏览(207)

我可以填写用户名/电子邮件和密码,但我不能点击个人安全复选框。
我已在此处选中有问题的复选框:

网址:https://www.trendyol.com/uyelik
以下是我迄今为止尝试过的方法:

from xml.etree.ElementPath import xpath_tokenizer
from selenium import webdriver
import requests

from selenium.webdriver.common.keys import Keys
import time
import random

def start():
    driver = webdriver.Chrome()
    driver.implicitly_wait(3)
    driver.get ('https://www.trendyol.com/uyelik')
    input('Harf gir.')
    mail = driver.find_element("xpath",'//*[@id="register-email"]')
    mail.send_keys("emrah.gumruk@gmail.com")
    time.sleep(0.5)
    password = driver.find_element("xpath",'//*[@id="register-password-input"]')
    time.sleep(0.5)
    password.send_keys("Emrah6161")
    male = driver.find_element ("xpath",'//*[@id="login-register"]/div[3]/div[1]/form/div[3]/div/button[2]')
    male.click()
    time.sleep(0.5)
    c1=driver.find_element ("xpath",'//*[@id="login-register"]/div[3]/div[1]/form/div[5]/div/div[1]/div/svg')
    c2=driver.find_element ("xpath",'//*[@id="login-register"]/div[3]/div[1]/form/div[6]/div/div[1]/div/svg')
    time.sleep(0.5)
    c1.click()
    c2.click()
    time.sleep(0.5)
    onay=driver.find_element ("xpath",'//*[@id="login-register"]/div[3]/div[1]/form/button')
    onay.click()
    time.sleep(20) 
start()

我尝试复制div的xpath并使用单击功能,但它不工作。

sg2wtvxw

sg2wtvxw1#

要单击与 * marketing-email * 相关的***clickable***元素,您需要为element_to_be_clickable()引入WebDriverWait,您可以使用以下locator strategies之一:

  • 使用 * CSS选择器 *:
driver.get('https://www.trendyol.com/uyelik')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
driver.find_element(By.CSS_SELECTOR, "div[name='marketing-email'] +svg.ty-check").click()
  • 使用 * XPATH *:
driver.get('https://www.trendyol.com/uyelik')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()
driver.find_element(By.XPATH, "//div[@name='marketing-email']//following::*[local-name()='svg' and @class='ty-check']").click()
      • 注意**:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
  • 浏览器快照:

相关问题