HTMLI was following an online tutorial on scraping glassdoor website via selenium.
My code does not get through this statement:
try:
driver.find_element_by_class_name("selected").click()
print('x out worked')
except ElementClickInterceptedException:
print('x out failed')
pass
time.sleep(.1)
try:
driver.find_element_by_css_selector('[alt="Close"]').click()
print(' x out worked')
except NoSuchElementException:
print(' x out failed (next page or missing)')
pass
The error I receive is :
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".selected"}
Couple of things that I tried:
driver.maximize_window()
driver.implicitly_wait(20)
Hard to tell without seeing the HTML, but try reversing your quotes (single/double)...
driver.find_element_by_css_selector("[alt='Close']").click()
Or...
driver.find_element_by_xpath("//*[#alt='Close']")
If that doesn't work add the relevant HTML section to your post
Related
So I have literally tried everything at this point.
I am getting the following error:
Error
The code that is causing the error is the below.
I have tried three difference excepts at this point, TimeoutException, NoSuchElementException and just a normal Exception, but neither works. If I uncomment the "raise NoSuchElementException" inside the try, it catches the error correctly, but not when it's the WebDriverWait that causes it?
Can someone please help :)
try:
#raise NoSuchElementException
WebDriverWait(driver, 15).until(lambda s: s.find_element_by_class_name("stream-preview__preview-image--loaded").is_displayed())
except TimeoutException:
print('NoSuchElementException:')
except NoSuchElementException:
print('NoSuchElementException')
except Exception as e:
print(e)
Seeing below error when trying to find element. Here I want to fill text after finding web element.
I am able to find xpath through chrome console, some how I am getting this issue. There are some posts on this issue, however most of them are related to appium, which is irrelevant to me.
util.driver.switchTo().defaultContent();
util.driver.switchTo().frame(0);
util.driver.findElement(By.xpath("//label[text()='Reason for Escalation']/following-sibling::div/input"));
Error message:
org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {error=no such element, message=no such element: Unable to locate element: {"method":"xpath","selector":"//label[text()='Reason for Escalation']/following-sibling::div/input"}
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:324)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:419)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)
Caused by: java.lang.ClassCastException: com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to org.openqa.selenium.WebElement
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:322)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:419)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)
There are 3 iframes on the page the elements am accessing are in first page
I would try to invoke WebDriverWait on the iframe before switching to it.
// wait for iframe to exist, then switch to it
WebDriverWait wait = new WebDriverWait(util.driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.Xpath("//iframe[contains(#name, 'vfFrameId')]")));
// wait for element to exist
element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//label[text()='Reason for Escalation']/following-sibling::div/input")));
I want to use selenium to automate the process from open a specific website to log in to search for particular articles. Few of the steps I could do it but facing error in 'sign in' step.
from selenium import webdriver
from selenium.webdriver.common.by import By
base = 'https://www.wsj.com'
url = 'https://www.wsj.com/search/term.html?KEYWORDS=cybersecurity&min-date=2018/04/01&max-date=2019/03/31&isAdvanced=true&daysback=90d&andor=AND&sort=date-desc&source=wsjarticle,wsjpro&page=1'
browser = webdriver.Safari(executable_path='/usr/bin/safaridriver')
browser.get(url)
browser.find_element_by_id('editions-select').click()
browser.find_element_by_id('na,us').click()
browser.find_element(By.XPATH, '//button[#type="button"],[contain(.,"Sign In")]').click()
browser.find_element_by_id('username').send_keys('**#&^&#$##$')
browser.find_element_by_id('password').send_keys('###$%%**')
browser.find_element_by_id('basic-login').click()
browser.find_element_by_id('masthead-container').click()
browser.find_element_by_id('searchInput').send_keys('cybersecurity')
browser.find_element_by_name('ADVANCED SEARCH').click()
browser.find_element_by_id('dp1560924131783').send_keys('2018/04/01')
browser.find_element_by_id('dp1560924131784').send_keys('2019/03/31')
browser.find_element_by_id('wsjblogs').click()
browser.find_element_by_id('wsjvideo').click()
browser.find_element_by_id('interactivemedia').click()
browser.find_element_by_id('sitesearch').click()
The code is working till this line:
browser.find_element_by_id('na,us').click()
But after that it is showing error in this line:
browser.find_element(By.XPATH, '//button[#type="button"],[contain(.,"Sign In")]').click()
The error message says​:
selenium.common.exceptions.InvalidSelectorException: Message:
What is wrong is my code?
This error message...
selenium.common.exceptions.InvalidSelectorException
...implies that the XPath expression was not a valid one.
However, it seems you were close. You need to replace:
'//button[#type="button"],[contain(.,"Sign In")]'
and join the two condition with and operator as follows:
"//button[#type='button' and contains(.,'Sign In')]"
Can anyone please tell me, if there is any mistake with my xpath. I have made it dynamic, to accept values from the parameter passed.
Xpath used in my code as :
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[#id='categories_block_left']/div[1]/ul[1]/li/a[contains(text(),'"+subCategory+"')]/Preceding-sibling::span[1]")));
Xpath I am getting on run time in the console after execution:
//div[#id='categories_block_left']/div[1]/ul[1]/li/a[contains(text(),'Tops')]/Preceding-sibling::span[1]
If I try to inspect above(runtime) by mentioning xpath in Firpath console, it is getting detected. But selenium is not able to detect this element.
Here is page URL:
http://automationpractice.com/index.php?id_category=3&controller=category
In the eclipse console I am getting error as :
Expected condition failed: waiting for presence of any elements located by By.xpath: //div[#id='categories_block_left']/div[1]/ul[1]/li/a[contains(text(),'Tops')]/Preceding-sibling::span[1]
SyntaxError: Failed to execute 'evaluate' on 'Document':
I'm using Cucumber with Watir Web-driver and Chrome browser.
When I execute my tests, sometimes there is an error like this:
"Selenium::WebDriver::Error::InvalidSelectorError: invalid selector: Unable to locate an element with the xpath expression //a[contains(., 'Joao Moreira')] because of the following error:
TypeError: Failed to execute 'createNSResolver' on 'Document': parameter 1 is not of type 'Node'.
(Session info: chrome=43.0.2357.81)
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.3 x86_64)"
I tried to get an answer trough Google but with no success.
Pretty sure this is this issue here: https://code.google.com/p/selenium/issues/detail?id=8600
And it is fixed as of Selenium 2.46.0. I haven't seen the error since moving.
Add a line to handle the exception thrown. Seems like the error halts the test. This has nothing to do with the locator, or iframe.Try to wrap your method in rescue clause:
begin
{your method}
rescue
Selenium::WebDriver::Error::InvalidSelectorError
end