I am new to Selenium and world of testing.
I have a query on identifying the objects using XPath/Firebug. I have a tree view which has folders and files when I click on any folder it recognizes with Node6...[n] etc. but I want to identify as its name and not Node. Here is the quick HTML code -
<a name="Node6" id="Node6" href="JAVASCRIPT:NodeClick(6, false );">Laptop Test</a>
<a name="Node7" id="Node7" href="JAVASCRIPT:NodeClick(7, false );">Laptop Main</a>
If I understand you correctly, you can use the following xpath:
//a[#name='Node6']
or
//a[text()='Laptop Test']
Related
I have a python script that opens a website with selenium .
After opening I want selenium to change some lines in html of some button in the website.
This is the actual code when I inspect the page with chrome dev tools
<button type="button" class="achieve-btn">Sometext & Sometext</button>
this is what I want to modify code with it
<button type="button" class="getExamButtonId1" onclick="openInstructionsPage(6012,'getExamButtonId1','incompleteExamId10001861')">Sometext & Sometext</button>
is it possible or not?
And if this is possible please provide the exact line of code to write in my script to do the following.
Yes it is possible with JS intervention :
Also I am assuming, that this css button[class='achieve-btn'] is unique in DOM.
element = driver.find_element_by_css_selector("button[class='achieve-btn']");
driver.execute_script("arguments[0].setAttribute('class', 'getExamButtonId1')", element);
similarly I would suggest to pass onclick attribute
You need to try and modify the innerHTML property for this element.
element = driver.find_element_by_css_selector("button.achieve-btn")
driver.execute_script("arguments[0].innerHTML = 'your_html_code_here'", element)
I am new to selenium, and I am studying how to extract what I want using selenium
I want to extract hyperlinks in a webpage, but only those that have specific tags. The hyperlink are all nested in the following structure:
<a title="Chris Frye" class="_32mo" href="https://www.facebook.com/CnMFrye"><span>Chris Frye</span></a>
However, when using tag 'a', I realized that it scrapes other hyperlinks, so I believe I need to condition both tag 'a' and 'class'.
In this case, what is the right strategy? I can't seem to use driver.find_elements_by_tag_name, because this is only for a single tag.
The page I want to scrape is : https://www.facebook.com/public/chris-frye
You can use css selector like bellow:
elements = driver.find_elements_by_css_selector('a._32mo')
Or use xpath:
elements = driver.find_elements_by_xpath("//a[#class='_32mo']")
So i have this element:
and i just can't find it, tried
driver.findElement(webdriver.By.partialLinkText('my_sites')).click();
but that just throws no such element error
I believe selecting by partial link text searches the visible text (the text in between the opening and closing a tag) rather than the href. Since you have no text within the a tag, it is not finding it. You would have to find using xpath something like "//a[contains(href, 'my_sites')]"
For selenium link text is what you find between the HTML brackets, for example:
LinkText
You can try to select via CSS selector:
driver.findElement(By.cssSelector("a[href*='my_sites']")).click();
Check this link for more info:
How to click a link whose href has a certain substring in Selenium?
I cannot identify the object of the icon showed in the attached screen shot. I have shown the HTML code as well.
The ID's are getting changed dynamically.
Can anyone guide on how to identity this kind of objects in Selenium?
If the ID is always changed, I recommend using CssSelector instead.
For instance,
<div id="running_number_12345" class="icon something">...</div>
You can use locator
driver.FindElement(By.CssSelector("div[class*='icon something']"));
If your icon doesn't have any specific css pattern, I recommend adding something in class attribute. If not, you have to use complex CssSelector to find it.
Try this
driver.findElement(By.cssSelector(".icon something"));
I have gotten the hang of using the html agility pack to find specific nodes using their attributes and xpaths. The problem is, I've been doing this manually for each of my projects (opening the website html and scanning for the nodes that have the text i need). Is there a way to select a single node by its inner text? This would make it easier to write an update script for websites whose content scheme is the same, but attribute tags change values over time. Thanks in advance!
Would be better if you have provided sample HTML, but since you haven't, let's assume we have HTML containing this markup :
<body>
<div class="foo">bar</div>
</body>
You can select the <div> by it's attribute using HtmlAgilityPack's SelectSingleNode() and XPath like so :
myHtmlDocument.DocumentNode.SelectSingleNode("//div[#class='foo']");
or you can select the same by the inner text like so :
myHtmlDocument.DocumentNode.SelectSingleNode("//div[.='bar']");
Hope this help.