Element:
<div class="rsl-MeetingHeader_RaceName " style="">Moe</div>
Attempt at code:
from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
chromedriver = r"C:\Users\\Downloads\Python\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(executable_path=r"C:\Users\\Downloads\Python\chromedriver_win32\chromedriver.exe",chrome_options=chromeOptions)
driver.get("https://www.bet365.com.au/#/AS/B2/")
track_button = driver.findElement(By.xpath("//div[#class='rsl-MeetingHeader_RaceName' and style='Moe']"
track_button.click()
track_button.click()
^
SyntaxError: invalid syntax
Keep getting a syntax error
You are missing the closing parens in this line
track_button = driver.findElement(By.xpath("//div[#class='rsl-MeetingHeader_RaceName' and style='Moe']"))
The next issue is going to be that the element is not located. Your XPath is looking for an element with style='Moe' but your element has style=""... the contained text is 'Moe'.
The class name in your element also contains a space at the end so you will need to use
#class='rsl-MeetingHeader_RaceName '
^
You can either look for the empty style like
track_button = driver.findElement(By.xpath("//div[#class='rsl-MeetingHeader_RaceName '][#style='']"))
or ignore style and look for contained text like
track_button = driver.findElement(By.xpath("//div[#class='rsl-MeetingHeader_RaceName '][text()='Moe']"))
If that still doesn't work, it's possible the page is still loading so you will need to add a WebDriverWait and wait until the element is clickable, then click it.
track_button = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='rsl-MeetingHeader_RaceName '][text()='Moe']"));
Below are some of the suggestions:
If the style is blank i.e. "", then you can use the below XPath:
//div[#style='']
If the style is not blank, then you can use the below XPath:
//div[#style='height: 0px;'][#class='fixed-nav-element-offset']
As per the code/HTML shared by you. You can use the below XPath as well:
//div[#class='rsl-MeetingHeader_RaceName ' and contains(text(),'Moe')]
OR
//div[#class='rsl-MeetingHeader_RaceName ' and contains(text(),'Moe')][#style='']
Related
String selector = ".rmcAlertDialog .buttons :first-child";
RemoteWebElement selection = (RemoteWebElement) driver.findElement(By.cssSelector(selector));
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(selection));
if (element == selection) selection.click();
But the element in question (a button) is not responding to the click.
If I click the button manually it works so its not the web page at fault, but the automation.
I have verified the button is there by comparing it's text content.
updated for clarification
This code works (or worked) for most buttons. The code is from a script interpreter which is parsing:-
select ".rmcAlertDialog .buttons :first-child" click
This code was working prior to more recent versions of chrome/selenium/chromedriver.
The code now doesn't work for some buttons.
selection.click() IS being called (verified in a debugger), as element will always equal selection, it just is not working.
.buttons is the class name of the container div for the button(s)
The selector is not directing to the element with button class. You have a space between .button and :first-child in the selector. Remove the space. The given selector is searching for a child element of the tag with button class. But I'm assuming you are trying to click on the first element with button class not the child node of the button class element.
Use this:
String selector = ".rmcAlertDialog .buttons:first-child";
I think the main reason it's failing is because your if statement will never be true. I've never done any comparisons like this but you can simplify your code significantly and still get the desired effect.
A few suggestions:
Don't define locators as Strings, define them as Bys. The By class is defined for just such a task and makes using and passing them around MUCH easier.
String selector = ".rmcAlertDialog .buttons:first-child";
would turn into
By locator = By.cssSelector(".rmcAlertDialog .buttons:first-child");
Note the correction that S Ahmed pointed out in his answer.
You don't need to find the element to wait for it to be clickable. There is an overload that takes a By locator, use that instead.
RemoteWebElement selection = (RemoteWebElement) driver.findElement(By.cssSelector(selector));
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(selection));
becomes
WebElement element = new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(locator));
Skip the RemoteWebElement and WebElement comparison. I don't think this will work and there's no need for it anyway. Your locator will locate the same element consistently.
So your final code should look something like
By locator = By.cssSelector(".rmcAlertDialog .buttons:first-child");
new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(locator)).click();
I'm very new to selenium and I'm experiencing an error when I'm going to select a colour from a colour palette.
When I'm trying to find that web element using XPath system returns an "Element not found" exception.
Someone please help me :)
IWebElement BGColorDropdown = driver.FindElement(By.XPath("/html/body/div[6]/div[2]/div/div[22]/span/span/span[2]/span"));
BGColorDropdown.Click();
System.Threading.Thread.Sleep(2000);
//Select value form "BG Color dropdown list"
IWebElement BGColorDropdownValue = driver.FindElement(By.XPath("//*[#id='4f9e73b0-6ffd-465c-bbee-7a8214e76a78']/div[3]/div/div/a"));BGColorDropdownValue.Click();
id attribute that you're trying to use in your XPath is dynamic. Try selectors from below code instead:
IWebElement BGColorDropdown = driver.FindElement(By.LinkText("Add new record"));
BGColorDropdown.Click();
System.Threading.Thread.Sleep(2000);
IWebElement BGColorDropdownValue = driver.FindElement(By.XPath("//div[#data-container-for='BG_COLOR']/following::span[#class='k-icon k-i-arrow-s']"));
BGColorDropdownValue.Click();
Also note that you should use relative XPath instead of absolute as it's more flexible, reliable and verbose
Using firefox and marking a link in my web-app I get among other things, this code which I think I can use to caprture an object:
cb_or_somename_someothername cb_area_0219
This string is "classname" in Firebug.
Going to the script I type in:
WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername cb_area_0219"));
But the script does not find the element when executing.
Other onfo in the Firebug panel is:
class="cb_or_somename_someothername cb_area_0219"
onclick="jsf.util.chain(this,event,'$(this).attr(\'disabled\', \'disabled\');return true;','mojarra.jsfcljs(document.getElementById(\'fwMainContentForm\'),{\'fwMainContentForm:j_idt156:2:selectRole \':\'fwMainContentForm:j_idt156:2:selectRole\'},\'\')');return false"
href="#"
id="fwMainContentForm:j_idt156:2:selectRole"
Is my script referring the element in a wrong way?
You cannot use search by compound class name (name with spaces). Try to use search by CSS selector instead:
WebElement rolle = driver.findElement(By.cssSelector(".cb_or_somename_someothername.cb_area_0219"));
or by XPath:
WebElement rolle = driver.findElement(By.xpath("//*[#class='cb_or_somename_someothername cb_area_0219']"));
Also you still can use search by one of two class names:
WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername"));
or
WebElement rolle = driver.findElement(By.className("cb_area_0219")); // Note that this class name could be generated dynamically, so each time it could has different decimal part
Update
If you get Element is not clickable... exception it seem that your element is covered by another element at the moment you try to click on it. So try to wait until this "cover" is no more visible:
new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[#class='environtment-banner']"));
WebElement rolle = driver.findElement(By.className("cb_area_0219"));
rolle.click();
I have followig HTML code and want X path for the text "Analytics & Research"
<div id="LLCompositePageContainer" class="column-wrapper">
<div id="compositePageTitleDiv">
<h1 class="page-header">Analytics & Research</h1>
</div>
I am getting following xpath using chrome, but that didnt work.
//*[#id="compositePageTitleDiv"]
this is my code
WebElement header = driver.findElement(By.xpath("//div[#id='LLCompositePageContainer']/div[#id='compositePageTitleDiv']/h1[#class='page-header']"));
String header2 = header.getText();
System.out.println(header2);
and following error I am getting
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to find element with xpath ==
//div[#id='LLCompositePageContainer']/div[#id='compositePageTitleDiv']/h1[#class='page-header']
(WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.34 seconds For documentation on this
error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html
Please try to use the below xpath:
driver.findElement(By.xpath(".//div[#id='compositePageTitleDiv']/h1")).getText();
If the element is inside the iframe. Then use the below code:
// Switching to the frame
driver.switchTo().frame(<name>);
// Storing the value of the Analytics & Research
String text = driver.findElement(By.xpath(".//div[#id='compositePageTitleDiv']/h1")).getText();
// Switching back to original window
driver.switchTo().defaultContent();
Hope this helps.
This is how it can be used :
WebElement element= driver.findElement(By.xpath("//*[#id='compositePageTitleDiv']"));
Or in case it is nested, can be accessed like this as well
WebElement element = driver.findElement(By.xpath("//html/body/div[3]/div[3]/"));
this is just a rough syntax.
No need to use Xpath here if you could simply locate the element using By.id(). Asuming are using Java, you should try as below :-
WebElement el = drive.findElement(By.id("compositePageTitleDiv"));
String text = el.getText();
Edited :- If element not found, may it is timing issues you need to implement WebDriverWait to wait for element until visible on the page as below :-
WebDriverWait wait = new WebDriverWait(webDriver, implicitWait);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("compositePageTitleDiv")));
String text = el.getText();
Note :- if your element is inside any frame, you need to switch that frame before finding element as :- driver.switchTo().frame("your frame name or id");
Hope it helps..:)
You can also use
//div[#id='LLCompositePageContainer']/div[#id='compositePageTitleDiv']/
h1[contains(text(),'Analytics')]
This is the best way to reach to the specific web element, using contains minimize the chances of error.
The correct xpath is
//div[#id='LLCompositePageContainer']
/div[#id='compositePageTitleDiv']
/h1[#class='page-header']
But you could have find your answer easily with some researchs on google...
How can I get the text 200 in this case:
<p style="margin:0">
You can get <big class="tooltip">200</big> more</p>
I'm trying using:
ReadOnlyCollection<IWebElement> list = _driver.FindElements(By.XPath("//*[contains(#class, 'tooltip')]"));
But can not get the text because the .Text property returns an empty string :(
Any thoughts?
You are using ClassName() with Xpath
ReadOnlyCollection<IWebElement> list = _driver.FindElements(By.Xpath("//*[#class=\"tooltip\"]/big"));
However, I would use CssSelector in this case since css is always preferred over xpath
ReadOnlyCollection<IWebElement> list = _driver.FindElements(By.CssSelector("big.tooltip"));
Considering the element is not inside any iframe since it is not throwing any error, I am assuming the wait is needed. Try the following
By byCss = By.CssSelector("big.tooltip");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
return d.FindElement(byCss);
});
var text = myDynamicElement.Text;
Looks the element is hidden. So JavaScript is probably your best try.
var text = ((IJavaScriptExecutor)driver).ExecuteScript("return document.querySelector('.tooltip_health_limit').innerHTML;") as string;
To get the text after you find the element:
string text = currentElement.Text;