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(',','')...
Changing display property for a hidden text area element with Playwright in Python
playwright waiting leads an error because of not responding click event
How to handle multiple pages with playwright-python?
Playwright: click on element within one/multiple elements using Python
How to make Playwright not to raise exceptions when the browser is closed
How to run tests on Edge using the browser library in robot framework
Installing playwright in Docker image fails
playwright python how to get specific texts follwing sibling inner text value
Using Playwright for Python, how can I wait for a field / selector result to change
Can't import name 'sync_playwright' from 'playwright.sync_api' on MacOS and ubutunOS
Here is my solution:
page.eval_on_selector(
selector="textarea", # Modify the selector to fit yours.
expression="(el) => el.style.display = 'inline-block'",
)
Here is the document of eval_on_selector()
.
Check out the latest blogs from LambdaTest on this topic:
The year 2021 can be encapsulated as one major transition. In 2022, the current breakthroughs in the elusive fight to eliminate the COVID-19 pandemic are top of mind for enterprises globally. At the same time, we are witnessing recent strides in technological advancements as the world gets digitized. As a result, the year 2022 will see the resumption of massive changes in technology and digital transformation, driving firms to adapt and transform themselves perpetually.
Smartphones have changed the way humans interact with technology. Be it travel, fitness, lifestyle, video games, or even services, it’s all just a few touches away (quite literally so). We only need to look at the growing throngs of smartphone or tablet users vs. desktop users to grasp this reality.
With the rapidly evolving technology due to its ever-increasing demand in today’s world, Digital Security has become a major concern for the Software Industry. There are various ways through which Digital Security can be achieved, Captcha being one of them.Captcha is easy for humans to solve but hard for “bots” and other malicious software to figure out. However, Captcha has always been tricky for the testers to automate, as many of them don’t know how to handle captcha in Selenium or using any other test automation framework.
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Locators Tutorial.
It is essential for a team, when speaking about test automation, to take the time needed to think, analyze and try what will be the best tool, framework, and language that suits your team’s needs.
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!!