简体   繁体   中英

how do I get a Specific part of a web attribute in python with selenium?

I need to get that part after window.open ('/echipa/lok-moscova/Sjs63WfK') as a string from this web element with selenium and I don't really know how to do it . if I can do it.

<a href="#" class="participant-imglink" onclick="window.open('/echipa/lok-moscova/Sjs63WfK'); return false;">Lok. Moscova</a>

You need to find the element in selenium. The easiest way is by id, by you can search by a lot of things (check more here ).

linkElement = driver.findElement(By.id("id"))

Next, you can extract attribute as a string

text = linkElement.getAttribute("onclick");

and remove obsolete parts

text = text.replace("window.open(", "").replace(")", "")

and that would be your "/echipa/lok-moscova/Sjs63WfK"

Here is example with BeautifulSoup (you can create soup object from selenium page source):

import re
from bs4 import BeautifulSoup

txt = '''
    <a href="#" class="participant-imglink" onclick="window.open('/echipa/lok-moscova/Sjs63WfK'); return false;">Lok. Moscova</a>
'''

soup = BeautifulSoup(txt, 'html.parser')

link = soup.select_one('a.participant-imglink[onclick]')
url = re.search(r"window\.open\('(.*?)'\)", link['onclick']).group(1)
print(url)

Prints:

/echipa/lok-moscova/Sjs63WfK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM