Best Python code snippet using playwright-python
KemdikbudScrapper.py
Source: KemdikbudScrapper.py
...13 doc = self.getHtmlResponse(self.tingkatan,self.tahun_ajaran,"1")14 table_description = doc.xpath('//table/thead/tr')15 table_description = table_description[0:len(table_description)-2]16 extracted_table_data = {}17 extracted_table_data['id_tabel'] = table_description[0].xpath('th')[0].text_content().replace('TABEL / TABLE : ','')18 extracted_table_data['tahun'] = table_description[3].text_content().replace('TAHUN / YEAR : ','')19 extracted_table_data['nama_tabel'] = table_description[1].text_content()20 raw_data = doc.xpath('//table/tbody/tr')21 for data in raw_data:22 cols = data.xpath('td')23 if len(cols[0].text_content()) >1:24 extracted_table_data[cols[1].text_content().split("/")[0].strip()] = {}25 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['negeri'] = cols[2].text_content().replace(',','')26 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['swasta'] = cols[4].text_content().replace(',','')27 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['jumlah'] = cols[6].text_content().replace(',','')28 return extracted_table_data29 def getGambaranUmumSekolahPerProvinsi(self):30 doc = self.getHtmlResponse(self.tingkatan,self.tahun_ajaran,"2")31 table_description = doc.xpath('//table/thead/tr')32 table_description = table_description[0:len(table_description)-2]33 extracted_table_data = {}34 raw_data = doc.xpath('//table/tbody/tr')35 for data in raw_data:36 cols = data.xpath('td')37 if len(cols[0].text_content()) >1:38 extracted_table_data[cols[1].text_content().split("/")[0].strip()] = {}39 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Sekolah'] = cols[2].text_content().replace(',','')40 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Siswa'] = cols[3].text_content().replace(',','')41 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Mengulang'] = cols[4].text_content().replace(',','')42 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Putus Sekolah'] = cols[5].text_content().replace(',','')43 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['KS & Guru'] = cols[6].text_content().replace(',','')44 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Tenaga Kependidikan'] = cols[7].text_content().replace(',','')45 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Rombel'] = cols[8].text_content().replace(',','')46 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Ruang Kelas'] = cols[9].text_content().replace(',','')47 return extracted_table_data48 def getGambaranUmumSekolahPerProvinsiPerWaktuPenyelenggaraan(self):49 doc = self.getHtmlResponse(self.tingkatan,self.tahun_ajaran,"3")50 table_description = doc.xpath('//table/thead/tr')51 table_description = table_description[0:len(table_description)-2]52 extracted_table_data = {}53 raw_data = doc.xpath('//table/tbody/tr')54 for data in raw_data:55 cols = data.xpath('td')56 if len(cols[0].text_content()) >1:57 extracted_table_data[cols[1].text_content().split("/")[0].strip()] = {}58 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Pagi'] = cols[2].text_content().replace(',','')59 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Siang'] = cols[4].text_content().replace(',','')60 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Kombinasi'] = cols[6].text_content().replace(',','')61 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Jumlah'] = cols[8].text_content().replace(',','')62 return extracted_table_data 63 def getJumlahSiswaMenurutAgamaTiapProvinsi(self):64 doc = self.getHtmlResponse(self.tingkatan,self.tahun_ajaran,"4")65 table_description = doc.xpath('//table/thead/tr')66 table_description = table_description[0:len(table_description)-2]67 extracted_table_data = {}68 raw_data = doc.xpath('//table/tbody/tr')69 for data in raw_data:70 cols = data.xpath('td')71 if len(cols[0].text_content()) >1:72 extracted_table_data[cols[1].text_content().split("/")[0].strip()] = {}73 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Islam'] = cols[2].text_content().replace(',','')74 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Protestan'] = cols[3].text_content().replace(',','')75 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Katolik'] = cols[4].text_content().replace(',','')76 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Hindu'] = cols[5].text_content().replace(',','')77 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Budha'] = cols[6].text_content().replace(',','')78 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Konghuchu'] = cols[7].text_content().replace(',','')79 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Lainnya'] = cols[8].text_content().replace(',','')80 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Jumlah'] = cols[9].text_content().replace(',','')81 return extracted_table_data82 def getJumlahSiswaMenurutjenisKelaminDanStatusSekolahTiapProvinsi(self):83 doc = self.getHtmlResponse(self.tingkatan,self.tahun_ajaran,"5")84 table_description = doc.xpath('//table/thead/tr')85 table_description = table_description[0:len(table_description)-2]86 extracted_table_data = {}87 raw_data = doc.xpath('//table/tbody/tr')88 for data in raw_data:89 cols = data.xpath('td')90 if len(cols[0].text_content()) >1:91 extracted_table_data[cols[1].text_content().split("/")[0].strip()] = {}92 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Perempuan'] = {}93 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['LP'] = {}94 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Laki-Laki'] = {}95 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Laki-Laki']['Negeri'] = cols[2].text_content().replace(',','')96 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Laki-Laki']['Swasta'] = cols[3].text_content().replace(',','')97 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Laki-Laki']['Subjml'] = cols[4].text_content().replace(',','')98 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Perempuan']['Negeri'] = cols[6].text_content().replace(',','')99 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Perempuan']['Swasta'] = cols[7].text_content().replace(',','')100 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Perempuan']['Subjml'] = cols[8].text_content().replace(',','')101 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['LP']['Negeri'] = cols[10].text_content().replace(',','')102 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['LP']['Swasta'] = cols[11].text_content().replace(',','')103 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['LP']['Subjml'] = cols[12].text_content().replace(',','')...
body_template1.py
Source: body_template1.py
1# coding: utf-82#3# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.4#5# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file6# except in compliance with the License. A copy of the License is located at7#8# http://aws.amazon.com/apache2.0/9#10# or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for12# the specific language governing permissions and limitations under the License.13#14import pprint15import re # noqa: F40116import six17import typing18from enum import Enum19from ask_sdk_model.interfaces.display.template import Template20if typing.TYPE_CHECKING:21 from typing import Dict, List, Optional22 from datetime import datetime23 from ask_sdk_model.interfaces.display.back_button_behavior import BackButtonBehavior24 from ask_sdk_model.interfaces.display.image import Image25 from ask_sdk_model.interfaces.display.text_content import TextContent26class BodyTemplate1(Template):27 """28 :param token: 29 :type token: (optional) str30 :param back_button: 31 :type back_button: (optional) ask_sdk_model.interfaces.display.back_button_behavior.BackButtonBehavior32 :param background_image: 33 :type background_image: (optional) ask_sdk_model.interfaces.display.image.Image34 :param title: 35 :type title: (optional) str36 :param text_content: 37 :type text_content: (optional) ask_sdk_model.interfaces.display.text_content.TextContent38 """39 deserialized_types = {40 'object_type': 'str',41 'token': 'str',42 'back_button': 'ask_sdk_model.interfaces.display.back_button_behavior.BackButtonBehavior',43 'background_image': 'ask_sdk_model.interfaces.display.image.Image',44 'title': 'str',45 'text_content': 'ask_sdk_model.interfaces.display.text_content.TextContent'46 }47 attribute_map = {48 'object_type': 'type',49 'token': 'token',50 'back_button': 'backButton',51 'background_image': 'backgroundImage',52 'title': 'title',53 'text_content': 'textContent'54 }55 def __init__(self, token=None, back_button=None, background_image=None, title=None, text_content=None):56 # type: (Optional[str], Optional[BackButtonBehavior], Optional[Image], Optional[str], Optional[TextContent]) -> None57 """58 :param token: 59 :type token: (optional) str60 :param back_button: 61 :type back_button: (optional) ask_sdk_model.interfaces.display.back_button_behavior.BackButtonBehavior62 :param background_image: 63 :type background_image: (optional) ask_sdk_model.interfaces.display.image.Image64 :param title: 65 :type title: (optional) str66 :param text_content: 67 :type text_content: (optional) ask_sdk_model.interfaces.display.text_content.TextContent68 """69 self.__discriminator_value = "BodyTemplate1"70 self.object_type = self.__discriminator_value71 super(BodyTemplate1, self).__init__(object_type=self.__discriminator_value, token=token, back_button=back_button)72 self.background_image = background_image73 self.title = title74 self.text_content = text_content75 def to_dict(self):76 # type: () -> Dict[str, object]77 """Returns the model properties as a dict"""78 result = {}79 for attr, _ in six.iteritems(self.deserialized_types):80 value = getattr(self, attr)81 if isinstance(value, list):82 result[attr] = list(map(83 lambda x: x.to_dict() if hasattr(x, "to_dict") else84 x.value if isinstance(x, Enum) else x,85 value86 ))87 elif isinstance(value, Enum):88 result[attr] = value.value89 elif hasattr(value, "to_dict"):90 result[attr] = value.to_dict()91 elif isinstance(value, dict):92 result[attr] = dict(map(93 lambda item: (item[0], item[1].to_dict())94 if hasattr(item[1], "to_dict") else95 (item[0], item[1].value)96 if isinstance(item[1], Enum) else item,97 value.items()98 ))99 else:100 result[attr] = value101 return result102 def to_str(self):103 # type: () -> str104 """Returns the string representation of the model"""105 return pprint.pformat(self.to_dict())106 def __repr__(self):107 # type: () -> str108 """For `print` and `pprint`"""109 return self.to_str()110 def __eq__(self, other):111 # type: (object) -> bool112 """Returns true if both objects are equal"""113 if not isinstance(other, BodyTemplate1):114 return False115 return self.__dict__ == other.__dict__116 def __ne__(self, other):117 # type: (object) -> bool118 """Returns true if both objects are not equal"""...
list_item.py
Source: list_item.py
1# coding: utf-82#3# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.4#5# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file6# except in compliance with the License. A copy of the License is located at7#8# http://aws.amazon.com/apache2.0/9#10# or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for12# the specific language governing permissions and limitations under the License.13#14import pprint15import re # noqa: F40116import six17import typing18from enum import Enum19if typing.TYPE_CHECKING:20 from typing import Dict, List, Optional21 from datetime import datetime22 from ask_sdk_model.interfaces.display.image import Image23 from ask_sdk_model.interfaces.display.text_content import TextContent24class ListItem(object):25 """26 :param token: 27 :type token: (optional) str28 :param image: 29 :type image: (optional) ask_sdk_model.interfaces.display.image.Image30 :param text_content: 31 :type text_content: (optional) ask_sdk_model.interfaces.display.text_content.TextContent32 """33 deserialized_types = {34 'token': 'str',35 'image': 'ask_sdk_model.interfaces.display.image.Image',36 'text_content': 'ask_sdk_model.interfaces.display.text_content.TextContent'37 }38 attribute_map = {39 'token': 'token',40 'image': 'image',41 'text_content': 'textContent'42 }43 def __init__(self, token=None, image=None, text_content=None):44 # type: (Optional[str], Optional[Image], Optional[TextContent]) -> None45 """46 :param token: 47 :type token: (optional) str48 :param image: 49 :type image: (optional) ask_sdk_model.interfaces.display.image.Image50 :param text_content: 51 :type text_content: (optional) ask_sdk_model.interfaces.display.text_content.TextContent52 """53 self.__discriminator_value = None54 self.token = token55 self.image = image56 self.text_content = text_content57 def to_dict(self):58 # type: () -> Dict[str, object]59 """Returns the model properties as a dict"""60 result = {}61 for attr, _ in six.iteritems(self.deserialized_types):62 value = getattr(self, attr)63 if isinstance(value, list):64 result[attr] = list(map(65 lambda x: x.to_dict() if hasattr(x, "to_dict") else66 x.value if isinstance(x, Enum) else x,67 value68 ))69 elif isinstance(value, Enum):70 result[attr] = value.value71 elif hasattr(value, "to_dict"):72 result[attr] = value.to_dict()73 elif isinstance(value, dict):74 result[attr] = dict(map(75 lambda item: (item[0], item[1].to_dict())76 if hasattr(item[1], "to_dict") else77 (item[0], item[1].value)78 if isinstance(item[1], Enum) else item,79 value.items()80 ))81 else:82 result[attr] = value83 return result84 def to_str(self):85 # type: () -> str86 """Returns the string representation of the model"""87 return pprint.pformat(self.to_dict())88 def __repr__(self):89 # type: () -> str90 """For `print` and `pprint`"""91 return self.to_str()92 def __eq__(self, other):93 # type: (object) -> bool94 """Returns true if both objects are equal"""95 if not isinstance(other, ListItem):96 return False97 return self.__dict__ == other.__dict__98 def __ne__(self, other):99 # type: (object) -> bool100 """Returns true if both objects are not equal"""...
processor.py
Source: processor.py
...6 doc = getHtmlResponse(tingkatan,tahun_ajaran,"1")7 table_description = doc.xpath('//table/thead/tr')8 table_description = table_description[0:len(table_description)-2]9 extracted_table_data = {}10 extracted_table_data['id_tabel'] = table_description[0].xpath('th')[0].text_content().replace('TABEL / TABLE : ','')11 extracted_table_data['tahun'] = table_description[3].text_content().replace('TAHUN / YEAR : ','')12 extracted_table_data['nama_tabel'] = table_description[1].text_content()13 raw_data = doc.xpath('//table/tbody/tr')14 for data in raw_data:15 cols = data.xpath('td')16 if len(cols[0].text_content()) >1:17 extracted_table_data[cols[1].text_content().split("/")[0].strip()] = {}18 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['negeri'] = cols[2].text_content().replace(',','')19 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['swasta'] = cols[4].text_content().replace(',','')20 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['jumlah'] = cols[6].text_content().replace(',','')21 return extracted_table_data22def getGambaranUmumSekolahPerProvinsi(tingkatan,tahun_ajaran):23 doc = getHtmlResponse(tingkatan,tahun_ajaran,"2")24 table_description = doc.xpath('//table/thead/tr')25 table_description = table_description[0:len(table_description)-2]26 extracted_table_data = {}27 extracted_table_data['id_tabel'] = table_description[0].xpath('th')[0].text_content().replace('TABEL / TABLE : ','')28 extracted_table_data['tahun'] = table_description[4].text_content().split(':')[1].strip()29 extracted_table_data['nama_tabel'] = table_description[1].text_content().strip()30 raw_data = doc.xpath('//table/tbody/tr')31 for data in raw_data:32 cols = data.xpath('td')33 if len(cols[0].text_content()) >1:34 extracted_table_data[cols[1].text_content().split("/")[0].strip()] = {}35 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Sekolah'] = cols[2].text_content().replace(',','')36 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Siswa'] = cols[3].text_content().replace(',','')37 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Mengulang'] = cols[4].text_content().replace(',','')38 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Putus Sekolah'] = cols[5].text_content().replace(',','')39 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['KS & Guru'] = cols[6].text_content().replace(',','')40 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Tenaga Kependidikan'] = cols[7].text_content().replace(',','')41 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Rombel'] = cols[8].text_content().replace(',','')42 extracted_table_data[cols[1].text_content().split("/")[0].strip()]['Ruang Kelas'] = cols[9].text_content().replace(',','')...
(Playwirght) Automation of actions is not working
Is there any way to close popup while running headless chromium?
Playwright auto-scroll to bottom of infinite-scroll page
Error message saying "AttributeError: 'NoneType' object has no attribute 'text'"
Using Playwright for Python, how do I select (or find) an element?
Playwright and PM2 Issue - hang while creating PlaywrightContextManager
Docker image error: /bin/sh: 1: python: not found
playwright python how to get specific texts follwing sibling inner text value
Why is it so difficult to add a cookie to an http cookiejar in python? I always end up with CookieError: Attempt to set a reserved key 'domain'
How do I read the content of a span class with python playwright
Try this:
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()
The problem was that you were searching specifically for the text "Morocco" to be present. It would not have been there if you were from another country since alibaba uses country detection. Your last code line, i.e. page.locator("text=Save").click()
was also faulty. The text "Save" resided inside a descendant element which you need to account for.
In general, it is always a good idea to make your locaters as general and flexible as possible to account for minor website changes done by the admin. Therefore, instead of using text to find the elements, maybe you should try using CSS selectors instead?
Further reading
Check out the latest blogs from LambdaTest on this topic:
Websites and web apps are growing in number day by day, and so are the expectations of people for a pleasant web experience. Even though the World Wide Web (WWW) was invented only in 1989 (32 years back), this technology has revolutionized the world we know back then. The best part is that it has made life easier for us. You no longer have to stand in long queues to pay your bills. You can get that done within a few minutes by visiting their website, web app, or mobile app.
In today’s data-driven world, the ability to access and analyze large amounts of data can give researchers, businesses & organizations a competitive edge. One of the most important & free sources of this data is the Internet, which can be accessed and mined through web scraping.
Playwright is a framework that I’ve always heard great things about but never had a chance to pick up until earlier this year. And since then, it’s become one of my favorite test automation frameworks to use when building a new automation project. It’s easy to set up, feature-packed, and one of the fastest, most reliable frameworks I’ve worked with.
The web development industry is growing, and many Best Automated UI Testing Tools are available to test your web-based project to ensure it is bug-free and easily accessible for every user. These tools help you test your web project and make it fully compatible with user-end requirements and needs.
A good User Interface (UI) is essential to the quality of software or application. A well-designed, sleek, and modern UI goes a long way towards providing a high-quality product for your customers − something that will turn them on.
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!