Lets say I have a list of links and want to click a link at random:
<div id="divA">
<a> first link </a>
<a> second link </a>
...
</div>
It isn't the smartest of ways (and if you have a better solution please tell me) but what I currently do is (roughly):
l = []
for i in range(numOfLinks):
xpath = '//div[#id="divA"]/a[%d]'%i
txt = sel.getText(xpath)
l.append(xpath, txt)
xpath,linkName = random.choice(l)
sel.click(xpath)
The main problem of this solution is that it sends many requests to selenium. My question is: is there a way of saving all these requests in a buffer and sending them at once?
are you using the text for anything?
numOfLinks = sel.get_xpath_count('//div[#id="divA"]/a')
random.randrange(1,numOfLinks)
sel.click('//div[#id="divA"]/a[%d]'%random.randrange(1,numOfLinks))
The code above will always click on a random link without having to get the text of the link each time.
Related
I updated the question after it was answered!
I try to find a text in a list on the webpage, which contains a html tag like <p> text </p>.
Heres a screenshot how it does look like on the webpage:
Screenshot of text to search for
Inside the "inspect" i used //*[text()='<p> First do this, then this</p>'] which could be found as seen above in the screenshot.
In the code im using this codeline to find the text:
webDriver.FindElement(By.XPath("//*[text()='<p> First do this, then this</p>']"))
But during the test run it gives this error message:
OpenQA.Selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[text()='
First do this, then this']"}
As you can see, selenium does somehow ignore the html tags <p> </p>
Answer and solution from cruisepandey:
Thanks to #cruisepandey i know now, my text is inside a textnode.
The only way to get the text out is using this code:
var ele = webDriver.FindElement(By.XPath("//table[#class='mud-table-root']//tbody/tr[1]/td[2]"));
Console.WriteLine(ele.Text);
The output of this here is:
<p> First do this, then this</p>
That's a text node, you cannot simply use text() method from xpath v1.0
You can try with below xpath :
(//table[#class='mud-table-root']//tbody/tr)[1]/td[2]
Code:
var ele = webDriver.FindElement(By.XPath("//table[#class='mud-table-root']//tbody/tr[1]/td[2]/text()"));
Console.WriteLine(ele.Text);
Code (With explicit waits) : in you want to click on it.
var ele = new WebDriverWait(webDriver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("(//table[#class='mud-table-root']//tbody/tr)[1]/td[2]")));
Console.WriteLine(ele.Text);
p is the element tag name, not part of it's text.
Also possibly there are spaces in the text. In this case I prefer using contains instead of exactly equation text check.
Try this instead:
webDriver.FindElement(By.XPath("//p[contains(text(),'First do this, then this')]"))
There are some posts about this topic but I cannot find any solution for my case, this is the situation:
I click on a link (a next page):
ActionChains(driver).move_to_element(next_el).click().perform()
Then I get the content of the new page(I'm interested on some script sections inside the body)
html = driver.find_element_by_xpath("//*").get_attribute("outerHTML")
But that content is always the same, no matter how long I wait for.
The only way to get the driver with new DOM information is to do a refresh(),
but for this case that is not a valid option.
Thanks and regards.
I am not sure what exactly you are looking for here, but if I am right you want to capture the content of script tag from the page.
If that is the case capture the page source in a string variable
sorce_code = driver.page_source , after you get the sting you can extract the value by any of the available string methods. I hope it helps.
I am trying to translate using microsoft cognitive text translation API:
Text to be translated is Your ticket with number INC123456 has been created. Following is the link, https://microsofttest.service-now.com/sp?id=ticket&table=incident&sys_id=aisuoiqwq1233444.
// Translating from english to telugu
This is getting translated into:
నెంబరు INC20534102 మీ టిక్కెట్ సృష్టించబడింది. దిగువ లింక్, [https://microsofttest.service-now.com/sp? id = టిక్కెట్ & పట్టిక = ఘటన & sys_id = aisuoiqwq1233444] (https://microsofttest.service-now.com/sp? id = టిక్కెట్ & పట్టిక = ఘటన & sys_id = aisuoiqwq1233444).
How can I prevent link from getting translated?
It is stated in the documentation that you can tag content so that it isn't translated using several methods.
I think the best method for your scenario is tagging your content with notranslate as per the example below:
<div class="notranslate">This will not be translated.</div>
<div>This will be translated. </div>
You can either allow the user to edit the content using HTML, but I think the most essential solution would be to write a function to search for URLs in the content and automatically add this notranslate tag to it and do the magic!
I am trying to identify a link (link text is 'Pearson eText') in Selenium. I have tried using By.linkText, By.xpath and even By.tagName but none of these seems to work. I was wondering if someone can let me know if there are more ways to identify an element. Following is the html code from the webpage. Thanks in advance!
<div class="container_12" style="margin-bottom:10px;">
<div class="grid_5" id="studlinks"><h3>Welcome to MasteringAandP</h3><p>Access all your Mastering course resources and assignments
</p><h3>Mastering Assignments</h3><p>Access Mastering homework assignments.</p><h3>Mastering Scores (for students)</h3><p>Access your results for all Mastering work you have completed. </p><h3>User Settings</h3><p>Manage your Pearson account information.</p><h3>Pearson eText</h3><p>Access your Pearson eText.</p><h3>Study Area</h3><p>Access the self Study Area to watch videos and animations, take practice quizzes and more. Your work in the Study Area does not report to your instructor's grade book. To complete instructor-assigned activities, go to Assignments.</p><h3>MasteringAandP Course Home</h3><p>Access your MasteringAandP course for additional content and assignments.</p></div>
<div class="grid_5" id="instlinks"></div>
</div>
There are several ways. One of the CSS selectors ways could be:
WebElement personETextLink = driver.findElement(By.cssSelector("a[href=\"javascript:launch('171')\"]"));
And, if you go for xpath it won't despair you.
WebElement personETextLink = driver.findElement(By.xpath("//*[#href=\"javascript:launch('171')\"]"));
Finding the Link element by xpath:
//a[text()='Pearson eText']
If you want to get the href, you can easily do:
//a[text()='Pearson eText']/#href
This is an example from a python book. When I run it I don't get any output. Can someone help me? Thanks!!!
from urllib import urlopen
from BeautifulSoup import BeautifulSoup
text = urlopen('https://python.org/community/jobs').read()
soup = BeautifulSoup(text)
jobs = set()
for header in soup('h3'):
links = header('a', 'reference')
if not links: continue
link = links[0]
jobs.add('%s (%s)' % (link.string, link['href']))
print jobs.add('%s (%s)' % (link.string, link['href']))
print '\n'.join(sorted(jobs, key=lambda s: s.lower()))
reedit--
firstly,i only considered the url is wrong but ignore the html infomation i wanna to get was not exist. May be this is why i get empty output.
If you open the page and inspect the html you'll notice there are no <h3> tags containing links. This is why you have no output.
So if not links: continue always continues.
This is probably because the page has moved to https://www.python.org/jobs/ so the <h3> tags containing links on the page are no longer present.
If you point this code's url to the new page. I'd suggest using taking some time to familiarize yourself with the page source. For instance it uses <h2> instead of <h3> tags for its links.