开发者

Clicking a link based on partial text match

开发者 https://www.devze.com 2023-02-27 07:47 出处:网络
I\'m using Sel开发者_运维百科enium 2/Webdriver with python and I want to click on the first link that starts with a string. Here\'s the code I came up with:

I'm using Sel开发者_运维百科enium 2/Webdriver with python and I want to click on the first link that starts with a string. Here's the code I came up with:

def click_link_partial(div_id, partial):
  linkdiv = driver.find_element_by_id(div_id)
  z = (a.click() for a in linkdiv.find_elements_by_tag_name('a') if a.text.startswith(partial))
  z.next()

I'm not very familiar with generators in Python. Why isn't a.click() called immediately, instead of when z.next() executes?

Are there any drawbacks to using this code?


First and foremost, please, familiarize yourself with Python's generators, they are a very powerful tool in your Python arsenal. A great explanation by Thomas Wouters can be found by reading another question: What can you use Python generator functions for?

Once you're finished reading, you'll realize that a generator just gives you the ability to evaluate expressions lazily.

Relating this piece of information to your code above, you will find that a.click() will not actually execute right away, because it is expected that you iterate over the generator expression, which is what you've created. This is why you must issue z.next() to actually invoke the click() method.

If you do not want to issue a z.next(), and assuming you just want to click the first partially matched link, you would re-write your code above as follows:

def click_link_partial(div_id, partial):
  linkdiv = driver.find_element_by_id(div_id)
  for a in linkdiv.find_elements_by_tag_name('a'):
      if a.text.startswith(partial):
         a.click()
         break  # stop iterating over the partially matched elements.

However, if you want to click on all the partially linked elements, then you should remove the z.next() from your code above and return the generator expression to be used in an outer function/method. Here's an example:

def click_link_partial(div_id, partial):
  linkdiv = driver.find_element_by_id(div_id)
  return (a for a in linkdiv.find_elements_by_tag_name('a') 
            if a.text.startswith(partial))

for matched_clickable_anchor in click_link_partial('#some-div-id', 'spam'):
    matched_clickable_anchor.click()  # do click
    # do something else.

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消