简体   繁体   中英

how to add web element into list in python

In the below code I'm extracting emails id and storing in the list in python

chrome_driver_path = os.path.abspath('..')  + "\\Drivers\\chromedriver.exe"

driver=webdriver.Chrome(chrome_driver_path)
driver.maximize_window()
driver.get("http://www.airindia.in/contact-details.htm")
driver.implicitly_wait(3)

doc = driver.page_source

emails = re.findall(r'[\w\.-]+@[\w\.-]+', doc)
list_new = []
for email in emails:
    list_new.extend(str(email))
    #print(email)

print("total emails - ",len(list_new))

driver.quit()

print(list_new)

but getting the output as ['c', 'a', 'l', 'l', '.', 'd', 'e', 'l', '@', 'a', 'i', 'r', 'i', 'n']

I need output as ['call.del@airindia.in','airindiaretros.ai@iclployalty.com']

I'm new to selenium python

sample code with my console output https://github.com/venkywarriors619/selenium_with_python/blob/master/Python_basics/SeleniumWebDriver_Advanced/RegularExpression.py

Use list.append() to append elements to a list.

Use list.extend() to append a bunch of elements (from a list of elements) to a list

Change

list_new.extend(str(email))

To:

list_new.append(str(email))

append adds its argument as a single element to the end of a list. The length of the list itself will increase by one.

extend iterates over its argument adding each element to the list, extending the list.

This post might help you.

You should not use regex for this purpose. Simply try below to get list of emails:

emails = [email.text for email in driver.find_elements_by_class_name('linkText') if "@" in email.text]

Extends() should become a list of elements as an input. That is why python threats your String as a list of characters and appends every single one to the list.

You should use append(). I takes one element and appends it on the end of a list.

Also I can recommend you to update your re pattern. I am using this one:

re_pattern = r'[\w\.-]+@[\w\.-]+\.[\w\.]+'

But take a look at "Email Address Regular Expression That 99.99% Works"

You use list.extend() which treats the string as a list of chars. Use list.append() instead to append the string as a single object.

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