简体   繁体   中英

Using Python with Playwright, how to get the value of an element?

I'm trying to get an email that is on a website, by copying the style, but I'm not having success with it. The process of navigating through the website and find the email is working perfectly, the only issue is to copy the email address.

I've tried to get the attribute of the field showing the email, but I'm getting the following error:

> Traceback (most recent call last):   File
> "c:\Users\RMBORGE\Desktop\PythonLib\tempCodeRunnerFile.python", line
> 21, in <module>
>     email = page.locator('xpath= //*[@id="cphMain_hlOrgInboundEmail"]').get_attribute() TypeError:
> get_attribute() missing 1 required positional argument: 'name'

Please see my code below:

from playwright.sync_api import sync_playwright
import time

el = "https://www.getmail.com"


p = sync_playwright().start()
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto(el)
owner = page.fill('xpath=//*[@id="cphMain_MyBA"]', "30291726")
search = page.locator('xpath=//*[@id="cphMain_btnSearch"]').click()
time.sleep(3)
name = page.locator('xpath=//*[@id="cphGrid_InvoicesGrid_PartnerOrgName_0"]').click()
#trying to get the attribute of the field
email = page.locator('xpath=//*[@id="cphMain_hlOrgInboundEmail"]').get_attribute()
print(email)

The code of the website was written like that:

"<a id="cphMain_hlOrgInboundEmail" href="mailto:email@gmail.com" style="font-weight:bold;">email@gmail.com</a>"

You have to use inner_text for this:

email = page.locator.inner_text("#cphMain_hlOrgInboundEmail")
print(email)

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