Python 如何在SeleniumWebDriver中查找和选择标记之间的元素?
这是我的bot.py代码:Python 如何在SeleniumWebDriver中查找和选择标记之间的元素?,python,html,selenium-chromedriver,Python,Html,Selenium Chromedriver,这是我的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')
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>
洋红是我想要选择的文字,也许有人想帮我
在候机楼我看到了
如果循环的
内部出现问题,它应该el。单击()
而不是aeelements。单击()
:
欢迎来到堆栈溢出。请查看如何创建。这也是一个很好的主意,学习和阅读。
AElements=driver.find_elements_by_tag_name('a')
for el in AElements:
if el.text == 'Magenta':
el.click()
#if you want break
break