By ID/Name VS. xpath Vs. link text

Id

Detail

Using an element ID or name locator

  1. Most efficient in terms of test performance
  2. Makes your test code more readable, assuming the ID or name within the page source is well-named

using XPath

  1. XPath statements take longer to process since the browser must run its XPath processor
  2. If the page source does not have an ID or name attribute you may have no choice but to use an XPath locator.
  3. With XPath (and DOM) you can locate an object with respect to another object on the page. For example, if there is a link that must occur within the second paragraph within a <div> section, you can use XPath to specify this. With ID and name locators, you can only specify that they occur on the page that is, somewhere on the page. If you must test that an image displaying the company logo appears at the top of the page within a header section, XPath may be the better locator.

Using link text

  1. Locating via a link’s text is often convenient and performs well.
  2. If the link’s text is likely to change frequently, locating by the <a> element would be the better choice.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化这段代码 import parsel import requests from time import sleep from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Firefox() driver.get('https://www.amazon.de/') word = input('请输入你需要的关键词:') driver.find_element(by=By.NAME, value="field-keywords").send_keys(word) sleep(5) driver.find_element(By.XPATH, "//input[@type='submit']").click() # 利用相对路径+属性值 driver.find_element(By.ID, "nav-search-submit-button").click() url = 'https://www.amazon.de/s?k={}'.format(word) headers = { 'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0', 'Referer': 'https://www.amazon.de/' } res = requests.get(url=url, headers=headers) html_data = res.text for links in driver.find_elements(By.XPATH, '//*[@class="a-link-normal s-underline-text s-underline-link-text s-link-style ' 'a-text-normal"]'): time.sleep(1) print(links.get_attribute('href')) a = [] for links in driver.find_elements(By.XPATH, '//*[@class="a-link-normal s-underline-text s-underline-link-text s-link-style ' 'a-text-normal"]'): time.sleep(1) print(links.get_attribute('href')) a.append(links.get_attribute('href')) driver.find_element(By.XPATH, '//*[@class="a-link-normal s-underline-text s-underline-link-text s-link-style ' 'a-text-normal"]').click() driver.find_element(By.ID, "sellerProfileTriggerId").click() box = driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/div/div/div/div/div[9]/div/div/div").text print(box) driver.back() driver.back() driver.refresh() for i in adriver.find_element(By.XPATH, '//*[@class="a-link-normal s-underline-text s-underline-link-text s-link-style ' 'a-text-normal"]').click(): print(i)
04-21

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值