【问题标题】:How to find and select element between tags in Selenium Webdriver?如何在 Selenium Webdriver 中的标签之间查找和选择元素?
【发布时间】:2019-11-25 13:09:49
【问题描述】:

这是我的 bot.py 代码:

from config import keys
from selenium import webdriver
import time


def order(k):

    driver= webdriver.Chrome("./chromedriver")



    driver.get(k['product_url'])
    driver.find_element_by_xpath('//*[@id="nav-categories"]/li[5]/a').click()
    AElements=driver.find_elements_by_tag_name('a')
    for el in AElements:
       if el.text == 'Magenta':
           AElements.click()

    time.sleep(0.5)
    driver.find_element_by_xpath('//*[@id="cart"]/a[2]').click()
    driver.find_element_by_xpath('//*[@id="order_billing_name"]').send_keys(k["name"])
    driver.find_element_by_xpath('//*[@id="order_email"]').send_keys(k["email"])
    driver.find_element_by_xpath('//*[@id="order_tel"]').send_keys(k["phone_number"])
    driver.find_element_by_xpath('//*[@id="bo"]').send_keys(k["address"])
    driver.find_element_by_xpath('//*[@id="order_billing_city"]').send_keys(k["city"])
    driver.find_element_by_xpath('//*[@id="order_billing_zip"]').send_keys(k["code"])
    driver.find_element_by_xpath('//*[@id="cnb"]').send_keys(k["card_number"])
    #month
    #driver.find_element_by_xpath("//*[@id="credit_card_month"]/option[{}]").format(k['card_exp_mo']).click()
    #year
    #driver.find_element_by_xpath("//*[@id="credit_card_year"]/option[{}]").format(k["card_exp_yr"]).click()
    driver.find_element_by_xpath('//*[@id="vval"]').send_keys(k["cvv_code"])
    driver.find_element_by_xpath('//*[@id="order_billing_country"]/option[26]').click()
    driver.find_element_by_xpath('// *[ @ id = "cart-cc"] / fieldset / p / label / div / ins').click()
    driver.find_element_by_xpath('//*[@id="pay"]/input').click()

if __name__ == "__main__":
    order(keys)

我想选择一个在 HTML 中显示为的元素:

<a class="name-link" href="/shop/tops-sweaters/tzdwbk5pg/dptqdojbx">Magenta</a>

洋红色是我想选择的文字,也许有人想帮助我

在终端中我看到了

【问题讨论】:

标签: python html selenium-chromedriver


【解决方案1】:

你的for loop 里面有问题,应该是el.click() 而不是AElements.click()

AElements=driver.find_elements_by_tag_name('a')
for el in AElements:
    if el.text == 'Magenta':
        el.click()
        #if you want break
        break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-10
    • 2017-08-05
    • 1970-01-01
    • 2014-02-20
    • 2012-12-12
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多