I have below fixture to navigate different navigate bar, but it will only work in the first time, when click next navigate bar, it will post the selenium.common.exceptions.TimeoutException: error.
#pytest.fixture()
def navigateToQueue(setup, request):
queuename = request.param
WebDriverWait(setup, 20).until(
EC.frame_to_be_available_and_switch_to_it((By.XPATH,
"//iframe[#title='Issuance']")))
setup.find_element_by_xpath("//mat-icon[contains(text(),'menu')]").click()
setup.find_element_by_xpath(f"//span[contains(text(),'{queuename}')]").click()
Related
I´m using selenium to scrape a webpage and it finds the elements on the main page, but when I use the click() function, the driver never finds the elements on the new page. I used beautifulSoup to see if it´s getting the html, but the html is always from the main. (When I see the driver window it shows that the page is opened).
html = driver.execute_script('return document.documentElement.outerHTML')
soup = bs.BeautifulSoup(html, 'html.parser')
print(soup.prettify)
I´ve used webDriverWait() to see if it´s not loading but even after 60 seconds it never does,
element = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.ID, "ddlProducto")))
also execute_script() to check if by clicking the button using javascript loads the page, but it returns None when I print a variable saving the new page.
selectProducto = driver.execute_script("return document.getElementById('ddlProducto');")
print(selectProducto)
Also used chwd = driver.window_handles and driver.switch_to_window(chwd[1]) but it says that the index is out of range.
chwd = driver.window_handles
driver.switch_to.window(chwd[1])
I'm having some issues in finding Selenium logs of actions performed on the browser.
My situation is the following:
I have a canvas with some clickable elements (it's a map).
I click on a specific point through actions.moveByOffset(pointToClick.getX(), pointToClick.getY()).click().build().perform(). Sometimes the point gets clicked, sometimes it seems not and I need to figure out what exactly happens.
How can I make sure that the click has indeed been performed? I set up the LoggingPreferences in the WebDriver configuration class
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
logPrefs.enable(LogType.PROFILER, Level.ALL);
logPrefs.enable(LogType.BROWSER, Level.ALL);
logPrefs.enable(LogType.CLIENT, Level.ALL);
logPrefs.enable(LogType.DRIVER, Level.ALL);
logPrefs.enable(LogType.SERVER, Level.ALL);
capabilities.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
but some of them (server, performance) are empty, while the browser logs contain data similar to what I see in the browser console. Since those are all the LogTypes available, I'm not even sure I'm looking at the correct direction. Do you have any suggestion?
Thank you for your help!
There are a few advanced libraries built-in to Selenium such as EventFiringWebDriver, AbstractEventListener, etc, which can be used to log all Selenium actions being performed.
Relevant Java Docs can be found here: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/events/EventFiringWebDriver.html
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/events/EventFiringDecorator.html
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/events/WebDriverListener.html
I also have a working Python SeleniumBase example from here, which performs actions that get recorded and printed out. To run that example, you'll first need to pip install seleniumbase, then run the test with pytest: pytest test_event_firing.py -s.
from selenium.webdriver.support.events import EventFiringWebDriver
from selenium.webdriver.support.events import AbstractEventListener
from seleniumbase import BaseCase
class MyListener(AbstractEventListener):
def before_navigate_to(self, url, driver):
print("Before navigating to: %s" % url)
def after_navigate_to(self, url, driver):
print("After navigating to: %s" % url)
def before_find(self, by, value, driver):
print('Before find "%s" (by = %s)' % (value, by))
def after_find(self, by, value, driver):
print('After find "%s" (by = %s)' % (value, by))
def before_click(self, element, driver):
print('Before clicking on element with text: "%s"' % element.text)
def after_click(self, element, driver):
print("Click complete!")
class EventFiringTests(BaseCase):
def test_event_firing_webdriver(self):
self.driver = EventFiringWebDriver(self.driver, MyListener())
print("\n* EventFiringWebDriver example *")
self.open("https://xkcd.com/1862/")
self.click("link=About")
self.open("https://store.xkcd.com/search")
self.type('input[name="q"]', "xkcd book\n")
self.open("https://xkcd.com/1822/")
The output of that prints the Selenium actions that were detected via the EventListener:
* EventFiringWebDriver example *
Before navigating to: https://xkcd.com/1862/
After navigating to: https://xkcd.com/1862/
Before find "About" (by = link text)
After find "About" (by = link text)
Before find "About" (by = link text)
After find "About" (by = link text)
Before clicking on element with text: "About"
Click complete!
Before navigating to: https://store.xkcd.com/search
After navigating to: https://store.xkcd.com/search
Before find "input[name="q"]" (by = css selector)
After find "input[name="q"]" (by = css selector)
Before navigating to: https://xkcd.com/1822/
After navigating to: https://xkcd.com/1822/
Similar solutions exist for the other Selenium language bindings.
From which place Selenium Webdriver usually gets title - using driver.title ?
from Page Source
or from DOM structure
title
title returns the title of the current page.
Usage:
title = driver.title
Defination:
def title(self):
"""Returns the title of the current page.
:Usage:
title = driver.title
"""
resp = self.execute(Command.GET_TITLE)
Details: When you invoke driver.title the HTTP GET request is invoked through the /session/{session id}/title URI Template.
NOTE: This command returns the document title of the current top-level browsing context, equivalent to calling document.title.
The remote end steps are:
If the current top-level browsing context is no longer open, return error with error code no such window.
Handle any user prompts and return its value if it is an error.
Let title be the result of calling the algorithm for getting the title attribute of the current top-level browsing context's active document.
Return success with data title.
I'm trying to scrape reviews from this site:
https://www.bbb.org/sacramento/business-reviews/heating-and-air-conditioning/elite-heating-air-conditioning-in-elk-grove-ca-47012326/reviews-and-complaints
But the content of the reviews isn't been loaded with by scrapy.
I tried then to use selenium to push the button and load the content:
url = 'https://www.bbb.org/losangelessiliconvalley/business-reviews/plumbers/bryco-plumbing-in-chatsworth-ca-13096711/reviews-and-complaints'
driver_1 = webdriver.Firefox()
driver_1.get(url)
content = driver_1.page_source
REVIEWS_BUTTON = '//*[#class="button orange first"]'
button = driver_1.find_element_by_xpath(REVIEWS_BUTTON)
button.click()
But selenium isn't able to find the button from the above xapth, I'm getting the following error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//*[#class=\"button orange first\"]"}
Your button located inside an iframe, so you need to switch to it first and then handle the button:
REVIEWS_BUTTON = '//*[#class="button orange first"]'
driver_1.switch_to_frame('the_iframe')
button = driver_1.find_element_by_xpath(REVIEWS_BUTTON)
button.click()
driver.switch_to_default_content()
Actions expected out of the code below :
User successfully logs in.
User moves to the top right corner of the website and clicks on the greeting link "Hi ....!".
Step 2 is not happening because the greeting hyperlink is not identified by WebDriver. What am I doing wrong?
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://flipkart.com");
driver.findElement(By.xpath(".//*[#id='container']/div/div/header/div[2]/div/div[1]/ul/li[8]/a")).click();
driver.findElement(By.xpath("//input[#class='fk-input login-form-input user-email']")).sendKeys("emailid");
driver.findElement(By.xpath("//input[#class='fk-input login-form-input user-pwd']")).sendKeys("password");
driver.findElement(By.xpath(".//*[#id='fk-mainbody-id']/div/div/div[1]/div/div[4]/div[7]/input")).click();
driver.findElement(By.linkText("Greeting _link")).click();
Error message:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Greeting _link"}
The HTML is:
<li class="_2sYLhZ _2mEF1S" data-reactid="26">
<a class="_1AHrFc _2k0gmP" data-reactid="27" href="#">Hi Neha!</a>
<ul class="_1u5ANM" data-reactid="28">
As I seeing after login there is no link which looks like as Hi username..!, but accroding to your comment I observe that you are talking about My account link which is visible in my case, I'm just rewriting your code which will automate from login to logout as below :-
driver.get("http://www.flipkart.com/");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Log In"))).click(); //it will click on login button
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.user-email"))).sendKeys("user name"); //it will fill user name
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.user-pwd"))).sendKeys('password'); //it will fill password
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.login-btn"))).click(); //it will click on login button
WebElement myAccount = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("My Account")));
Mouse mouse = ((HasInputDevices)driver).getMouse();
mouse.mouseMove(((Locatable)hoverElement).getCoordinates()); //it will perform mouse over on My Account link, if in your case it show as 'Hi Neha!' you can replace it.
wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Log Out"))).click(); //it will click on logout click after mouse over
Hope it helps..:)