我写了一个代码使用剧作家在全球速卖通网站上自动执行一些操作。
:https://www.aliexpress.com/wholesale?catId=0& initiative_id = AS_20220518080059& SearchText =化妆品
下面是脚本我编写了在解析站点数据之前自动更改国家的操作
from playwright.sync_api import sync_playwright
def main():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.aliexpress.com/wholesale?catId=0&initiative_id=AS_20220518080059&SearchText=makeup")
countrySwitcher = page.query_selector("#switcher-info")
countrySwitcher.click()
page.locator("a:has-text("Morocco")").click()
page.locator("[placeholder="Search"]").first.click()
page.locator("[placeholder="Search"]").first.fill("Spain")
page.locator("li:has-text("Spain")").click()
page.locator("span:has-text("Español")").click()
page.locator("text=English").nth(1).click()
page.locator("span:has-text("EUR ( Euro )")").click()
page.locator("text=EUR ( Euro ) CHF ( Swiss Franc ) MXN ( Mexican Peso ) EUR ( Euro ) CLP ( Chilean >> [placeholder="Search"]").click()
page.locator("text=EUR ( Euro ) CHF ( Swiss Franc ) MXN ( Mexican Peso ) EUR ( Euro ) CLP ( Chilean >> [placeholder="Search"]").fill("MAD")
page.locator("text=MAD ( Moroccan Dirham )").click()
with page.expect_navigation():
page.locator("text=Save").click()
if __name__ == '__main__':
main()
但是代码不工作,在我的控制台中显示这个错误:
File "C:UsersaichaAppDataLocalProgramsPythonPython310libsite-packagesplaywright_impl_connection.py", line 63, in inner_send
result = next(iter(done)).result()
playwright._impl._api_types.TimeoutError: Timeout 30000ms exceeded.
=========================== logs ===========================
waiting for selector "a:has-text("Morocco")"
============================================================
我认为代码停止编译时,它到达这一行countrySwitcher = page.query_selector("#switcher-info")
但我真的不知道为什么。我会感谢你的任何帮助来解决这个问题,因为我是很新的使用剧作家。谢谢!
试试这个:
def main():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.aliexpress.com/wholesale?catId=0&initiative_id=AS_20220518080059&SearchText=makeup")
countrySwitcher = page.query_selector("#switcher-info")
countrySwitcher.click()
# time.sleep(6000)
page.locator("#nav-global > div.ng-item-wrap.ng-item.ng-switcher.active > div > div > div > div.switcher-shipto.item.util-clearfix > div").click()
page.locator("[placeholder="Search"]").first.click()
page.locator("[placeholder="Search"]").first.fill("Spain")
page.locator("li:has-text("Spain")").click()
page.locator("span:has-text("Español")").click()
page.locator("text=English").nth(1).click()
page.locator("span:has-text("EUR ( Euro )")").click()
page.locator(
"text=EUR ( Euro ) CHF ( Swiss Franc ) MXN ( Mexican Peso ) EUR ( Euro ) CLP ( Chilean >> [placeholder="Search"]").click()
page.locator(
"text=EUR ( Euro ) CHF ( Swiss Franc ) MXN ( Mexican Peso ) EUR ( Euro ) CLP ( Chilean >> [placeholder="Search"]").fill(
"MAD")
page.locator("text=MAD ( Moroccan Dirham )").click()
with page.expect_navigation():
page.locator("button", has_text="Save").click()
if __name__ == '__main__':
main()
问题是您正在专门搜索文本"Morocco"活在当下。如果你来自另一个国家,它就不会出现在那里,因为阿里巴巴使用了国家检测。你的最后一行代码,即page.locator("text=Save").click()
也有问题。文本"保存";位于需要考虑的后代元素中。
总的来说,它总是一个好主意,使你的定位器尽可能通用和灵活,以解释由管理员完成的网站的小变化。因此,与其使用文本来查找元素,不如尝试使用CSS选择器。
进一步阅读