Codelenium @[email protected]
74 subscribers - no pronouns :c
in the future - u will be able to do some more stuff here,,,!! like pat catgirl- i mean um yeah... for now u can only see others's posts :c
π Selenium Tip of the Dayπ
Assert the URL after an action like login or navigation:
assert driver.getCurrentUrl().equals("example.com/dashboard");
β
Quick, clean, and ensures you're on the right page!
π§ͺ Use in `@Test` methods with TestNG for validation.
1 - 0
Selenium Tip of the Day:
When dealing with iframes in Selenium, always switch to the iframe before interacting with its elements. After you're done, switch back to the main content.
// Switch to iframe by ID, name, or WebElement
driver.switchTo().frame("iframeId");
// Perform actions inside the iframe
// Switch back to the main content
driver.switchTo().defaultContent();
This ensures you're interacting with elements inside the iframe correctly.
2 - 0
Selenium Tip of the Day: Use `WebDriverWait` with `ExpectedConditions` to handle dynamic elements instead of `Thread.sleep()`. It improves test reliability and speed! π
3 - 0
π‘ Selenium Tip of the Day: Debugging Your Tests
1. Use Breakpoints β Pause execution in your IDE to inspect variables.
2. Print Debugging β Log values with System.out.println() for Java.
3. Take Screenshots β Capture failures.
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("screenshot.png"));
4. Use Explicit Waits β Ensure elements load before interacting.
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.id("myElement")));
5. Check for Hidden Elements.
getAttribute("style") to verify visibility.
6. Enable Browser Logs β Catch console errors.
driver.manage().logs().get(LogType.BROWSER).forEach(log -> System.out.println(log.getMessage()));
Debug smarter, not harder! π
3 - 0
Thank you all for 105 hours of watch time! More videos and tips to come. π§βπ»π©βπ»π‘
3 - 0
π‘ Selenium Tip of the Day: How to Handle Dynamic Elements π‘
Dynamic elements in web applications change frequently, making them tricky to locate reliably.
Here are some strategies to handle them in Selenium:
------------------------------------------------------------------------------------------------------------------------------
1. Use Dynamic XPath with Contains or Starts-With
Instead of relying on static attributes, use XPath functions like contains() or starts-with():
WebElement element = driver.findElement(By.xpath("//button[contains(text(), 'Submit')]"));
WebElement element = driver.findElement(By.xpath("//input[starts-with(@id, 'user_')]"));
------------------------------------------------------------------------------------------------------------------------------
2. Use CSS Selectors for Partial Matching
If the id or class attribute changes dynamically, target stable parts:
WebElement element = driver.findElement(By.cssSelector("button[class*='submit-btn']"));
------------------------------------------------------------------------------------------------------------------------------
3. Implement Explicit Waits
Wait for elements to appear before interacting with them:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicElement")));
------------------------------------------------------------------------------------------------------------------------------
4. Use JavaScript Executor for Unreachable Elements
If elements are present but not interactable, try JavaScript:
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.id("dynamicElement"));
js.executeScript("arguments[0].click();", element);
------------------------------------------------------------------------------------------------------------------------------
5. Refresh the DOM Reference
If an element becomes stale due to page updates, re-locate it before interacting:
WebElement element = driver.findElement(By.id("dynamicElement"));
driver.navigate().refresh();
element = driver.findElement(By.id("dynamicElement")); // Re-locate after refresh
------------------------------------------------------------------------------------------------------------------------------
π By using these techniques, you can handle dynamic elements more effectively and avoid flaky tests! π
Did you find this helpful?
2 - 0
π‘Selenium Tip of the Day: How to Handle iFrames
When working with iFrames in Selenium, you need to switch to the frame before interacting with elements inside it.
Switch to an iFrame by Index:
driver.switchTo().frame(0); // Switch to the first iFrame (index starts from 0)
Switch to an iFrame by Name or ID:
driver.switchTo().frame("iframeName"); // Switch using the name or ID of the iFrame
Switch to an iFrame using WebElement:
WebElement iframeElement = driver.findElement(By.id("iframeID"));
driver.switchTo().frame(iframeElement); // Switch using a WebElement
Switch Back to the Main Page:
driver.switchTo().defaultContent(); // Exit the iFrame and return to the main page
Handling iFrames correctly ensures smooth interaction with elements inside embedded frames! π
1 - 0
π‘ Selenium Tip of the Day: How to Close a Tab
To close the current tab in Selenium, use:
driver.close();
This will close the active tab or window but wonβt quit the entire browser session. If you want to close all tabs and end the session, use:
driver.quit();
Bonus: If you opened multiple tabs and want to switch before closing, use:
ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size() - 1)); // Switch to the last opened tab
driver.close(); // Close the current tab
Mastering tab management keeps your automation clean and efficient!
2 - 0
π‘ Selenium Tip of the Day: Managing Browser Tabs
When automating tests involving multiple tabs, always track window handles.
Use the driver to switch between tabs seamlessly and ensure actions are performed on the correct tab.
This helps maintain consistency and avoid errors in your test flow.
4 - 0
Subscribe for more Selenium goodness!
Welcome to Codelenium! π
Your ultimate destination for mastering test automation, Selenium tutorials, and coding insights. Whether you're new to automation or looking to refine your skills, Iβve got you covered!
What You'll Learn
- Step-by-step Selenium tutorials π₯οΈ
- Tips for writing efficient test scripts π‘
- Debugging common Selenium issues π οΈ
Support the Channel
Love what I do? β€οΈ Your support helps me create more tutorials, tools, and resources!
π‘ Donβt forget to like, comment, and subscribe for weekly videos that take your Selenium skills to the next level!
#coding #selenium #automation #programming #chill #tutorial #java #eclipse #code #seleniumwebdriver #seleniumtekautomation #seleniumtips #seleniumjava #seleniumtutorialforbeginners #seleniumtesting #seleniumautomation #javaprogramming