I have the following html:
<code>The first code block</code>
<p>Some text and <code>the second code block</code> followed by other text</p>
I need to find and remove all code blocks from it. I use the following XPath '//code', but it finds only the first code block while the second remains.
Question: Why '//code' is not able to catch the second code block? How to fix it?
Details: I'm doing it in Ruby using Nokagiry. My code looks like this:
html = Nokogiri::HTML(File.read(htmlFile))
html.search('//code').remove
UPDATE:
The XPath worked in fact. I just made a mistake in different place.
Seems like You forget about iterator...
Try:
html = Nokogiri::HTML(File.read(htmlFile))
html.search('//code').each{|htm| htm.remove}
Related
I have a question related to Selenium in Python:
I want to obtain the text content "D. New Jersey" on a webpage. In addition, the text that I want to get can be different on different pages, but it is always under "COURT:".
The HTML code is:
<div class="span4">
<strong>COURT:</strong>
D. New Jersey
</div>
The code I use now is as follows. And it doesn't work.
self.driver.get(address)
element=driver.findElement("//a[contains(#class,'span4') and contains(div/div/text(),'COURT:')]").gettext()
I have also tried the following solutions with no luck, and no Selenium exception is being thrown either:
text = self.driver.find_element_by_xpath("//div[strong[text()='COURT:']]").text
and
text = self.driver.find_element_by_xpath("//a[contains(#class,'span4') and contains(div/div/text(),'COURT:')]").text
Is there anyone who knows how to get the text from this code using Selenium?
Thanks
For Python, you can get the text as such:
text = self.driver.find_element_by_xpath("//div[strong[text()='COURT:']]").text
This uses an XPath to query on the div element, using its inner strong element to ensure we have selected the correct div. Then, we call Python's webelement.text method to get the div's text.
Is there a way to force the <code> tag respect single lines in Mediawiki? I really don't want to have to use the Poem extension because then I think it looks ugly. For example:
<poem>
<code>
Here's a block of code.
With two lines.
</code>
</poem>
I think that looks a bit bad... Any suggestions on just allowing the code tag to do the trick alone?
Nevermind. Just simply use <pre class="code"> and this does exactly every single thing I want to do.
One can also use https://www.mediawiki.org/wiki/Extension:SyntaxHighlight which handles new lines fine, too.
Example code:
<syntaxhighlight lang="css">
.class {
some-css;
}
</syntaxhighlight>
Hey I have this code in one of my div elements:
<div class="col-sm-8">Account Information: </div>
Can someone tell me how I would go about finding this element in my protractor code? Is it possible to do something like this:
expect(element(by.divText('Account Information: ')).isDisplayed()).toBe(true);
I have multiple elements with the class "col-sm-8" so I am not able to find the element by class. I was just wondering if there is any way to possibly find the element using the text in the div element? Thanks for the help!
I would recommend you to use by.cssContainingText
element(by.cssContainingText('.col-sm-8', 'Account Information'))
There is no webdriver method which would allow locating an element by its text. You could try using xpath in the following way (not tested):
element(by.xpath('//div[contains(text(), "Account Information: ")]')
keep in mind by.cssContainingText matches the element by PARTIAL text
so element(by.cssContainingText('div', 'male')) will actually match both male and female text
To solve this, use xpath with exact text match
element(by.xpath('//div[text()="male"]'))
I'm trying to automate the Google Translate web interface with Selenium (but it's not necessary to understand Selenium to understand this question, just know that it finds elements and clicks them). I'm stuck on selecting the language to translate from.
I can't get to the point where the drop-down menu opens, as seen in the screenshot below.
Now, I want to select 'Japanese'.
This xpath expression works: $b.find_element(:xpath,"//*[#id=':13']/div").click But I would rather have one where I can just input the name of the language.
This xpath expression also works: $b.find_element(:xpath,"//*[contains(text(),'Japanese')]").click But only as long as there is no other 'Japanese' text on the page.
So I'm trying to narrow down the scope of my xpath, but when I try to specify the path to take to find the 'Japanese' text, the expression no longer works, I can't find the element: $b.find_element(:xpath,"//*div[#id='gt-sl-gms']/*[contains(text(),'Japanese')]").click
It also no longer works for the original xpath either: $b.find_element(:xpath,"//*div[#id='gt-sl-gms']/*[#id=':13']/div").click
Which is weird, because to bring down the drop-down menu, I use this xpath $b.find_element(:xpath,"//*[#id='gt-sl-gms']/*[contains(text(),'From:')]").click.
So it's not that I have two wildcards in my expression and it's not that my expression is too specific. There's something else that I'm missing and I'm sure it's really simple.
Any suggestions are appreciated.
Edit Other things I have tried unsuccessfully:
$b.find_element(:xpath,"//*/div[#id='gt-sl-gms']/*[#id=':13']/div").click
$b.find_element(:xpath,"//*[#id='gt-sl-gms']/*[#id=':13']/div").click
$b.find_element(:xpath,"//*[#id='gt-sl-gms']//*[#id=':13']/div").click
If the div with "#id=':13'" is an descendant of the div with "#id='gt-sl-gms" your xpaht "//*[#id='gt-sl-gms']//*[#id=':13']/div" would work.
The above xpaht expect that the html looks somehow like:
<div id="gt-sl-gms">
<div>
<div id=":13">
<div></div>
</div>
</div>
</div>
If <div id="gt-sl-gms"> in not an ancestor (as I expect) you have to look for an "real" ancestor, or you may use following (for nodes later in the document) or following-sibling (for nodes later in the document at the same level as the previous.
*div is incorrect, it should be just div. Also, depending on he structure of the HTML, you may need // instead of /.
Try selecting descendants (//) instead of (/*) which is really grandchildren or deeper.
What tag can I use to prevent any interpretation? I need that because I need to write down some source code and it's result in blogger. I have this code in blogspot, but the code inside the <pre> is processed
The code is as follows:
<pre class='prettyprint'>
$latex \displaystyle S(n)=\sum_{k=1}^{n}{\frac{1}{T_{k}}=\sum_{k=1}^{n}{\frac{6}{k(k+1)(k+2)}$
</pre>
This is the result:
$latex \displaystyle S(n)=\sum_{k=1}^{n}{\frac{1}{T_{k}}=\sum_{k=1}^{n}{\frac{6}{k(k+1)(k+2)}$
When I can replace '$' in <pre> with something equivalent, I could avoid this issue.
I tried <code> and <pre>, but they all interpret the content.
ADDED
I'm trying to use the javascript code found in this post.
If I understand correctly, you are using Replacemath, and its documentation says: “Should you need to to prevent certain $ signs from triggering LaTeX rendering, replace $ with the equivalent HTML <span>$</span> or $, or put the code inside a <pre> or <code> block if appropriate.” Of these, the first method seems to actually work.
That is, replace all occurrences of “$” inside the pre element by <span>$</span>.
I tested this by publishing a test in my blog (which had been dormant for 6 years...). I had to manually break the pre block to fit into the column.