0% found this document useful (0 votes)
49 views

Selenium by Using Python

Uploaded by

phonesjunction
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Selenium by Using Python

Uploaded by

phonesjunction
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

When working with Selenium in real-time scenarios, especially for practical web

automation and testing tasks, the scripts often address common needs such as form
submissions, data extraction, and interaction with dynamic content. Below are some
real-time examples of commonly asked Selenium scripts in Python:

### 1. **Login to a Website**

```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(executable_path='path/to/chromedriver')
driver.get('https://example.com/login')

# Find username and password fields and submit button


username = driver.find_element(By.ID, 'username')
password = driver.find_element(By.ID, 'password')
login_button = driver.find_element(By.ID, 'loginButton')

# Enter credentials and click login


username.send_keys('your_username')
password.send_keys('your_password')
login_button.click()

# Optionally, check if login was successful


assert "Dashboard" in driver.title

driver.quit()
```

### 2. **Scrape Data from a Table**

```python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path='path/to/chromedriver')
driver.get('https://example.com/data-table')

# Find the table


table = driver.find_element(By.ID, 'dataTable')

# Get all rows in the table


rows = table.find_elements(By.TAG_NAME, 'tr')

for row in rows:


columns = row.find_elements(By.TAG_NAME, 'td')
data = [col.text for col in columns]
print(data)

driver.quit()
```

### 3. **Interact with a Modal Popup**

```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(executable_path='path/to/chromedriver')
driver.get('https://example.com/page-with-modal')

# Trigger the modal


driver.find_element(By.ID, 'openModalButton').click()

# Wait for the modal to be visible


modal = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, 'modal'))
)

# Interact with elements inside the modal


modal.find_element(By.ID, 'modalInput').send_keys('Some text')
modal.find_element(By.ID, 'modalSubmit').click()

# Close the modal


driver.find_element(By.ID, 'modalClose').click()

driver.quit()
```

### 4. **Fill Out and Submit a Form**

```python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path='path/to/chromedriver')
driver.get('https://example.com/form')

# Find form fields


name_field = driver.find_element(By.ID, 'name')
email_field = driver.find_element(By.ID, 'email')
submit_button = driver.find_element(By.ID, 'submit')

# Fill out the form


name_field.send_keys('John Doe')
email_field.send_keys('[email protected]')
submit_button.click()

# Optionally, verify form submission


assert "Thank you" in driver.page_source

driver.quit()
```

### 5. **Handle Infinite Scrolling**

```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome(executable_path='path/to/chromedriver')
driver.get('https://example.com/infinite-scroll')
# Scroll down until the desired content is loaded
scroll_pause_time = 2
last_height = driver.execute_script("return document.body.scrollHeight")

while True:
driver.find_element(By.TAG_NAME, 'body').send_keys(Keys.END)
time.sleep(scroll_pause_time)
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height

# Optionally, extract information


items = driver.find_elements(By.CLASS_NAME, 'item')
for item in items:
print(item.text)

driver.quit()
```

### 6. **Take Screenshots of Specific Elements**

```python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path='path/to/chromedriver')
driver.get('https://example.com/some-page')

# Find the specific element


element = driver.find_element(By.ID, 'specific-element')

# Take a screenshot of the specific element


element.screenshot('element_screenshot.png')

driver.quit()
```

### 7. **Test Responsiveness by Emulating Mobile View**

```python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

# Set up Chrome options for mobile emulation


chrome_options = Options()
chrome_options.add_experimental_option('mobileEmulation', {
'deviceName': 'Nexus 5'
})

driver = webdriver.Chrome(service=Service('path/to/chromedriver'),
options=chrome_options)
driver.get('https://example.com')

# Verify content or layout


assert "Mobile View" in driver.title
driver.quit()
```

### 8. **Upload a File and Verify Upload**

```python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome(executable_path='path/to/chromedriver')
driver.get('https://example.com/upload')

# Find the file input element and upload a file


file_input = driver.find_element(By.ID, 'file-upload')
file_input.send_keys('path/to/file.txt')

# Optionally, verify file upload


time.sleep(2) # Wait for the upload to complete
uploaded_file = driver.find_element(By.ID, 'uploaded-file-name').text
assert 'file.txt' in uploaded_file

driver.quit()
```

### 9. **Interact with JavaScript Popups**

```python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path='path/to/chromedriver')
driver.get('https://example.com/javascript-popup')

# Trigger the JavaScript alert


driver.find_element(By.ID, 'trigger-alert').click()

# Switch to alert and accept


alert = driver.switch_to.alert
alert.accept()

driver.quit()
```

### 10. **Verify Presence of Elements After AJAX Calls**

```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(executable_path='path/to/chromedriver')
driver.get('https://example.com/ajax-page')

# Trigger AJAX call


driver.find_element(By.ID, 'load-more-button').click()
# Wait until the new content is loaded
new_content = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, 'new-content'))
)

assert new_content.is_displayed()

driver.quit()
```

These scripts cover various real-time use cases from login processes and form
submissions to dynamic content handling and responsiveness testing. Adapt these
examples as needed to fit your specific requirements and websites.

You might also like