I want to find a button, but don't know how as the html code is complicated. Can you help me?
HTML
<div style="margin-top: 20px;">
<span style="display: inline-block; margin: 5px; padding: 10px 15px; border-radius: 5px; font-size: 18px; line-height: 16px; cursor: pointer; color: rgb(255, 255, 255); background-color: rgb(99, 99, 102);">Bugsnag akzeptieren
</span>
<span style="display: inline-block; margin: 5px; padding: 10px 15px; border-radius: 5px; font-size: 18px; line-height: 16px; cursor: pointer; color: rgb(255, 255, 255); background-color: rgb(0, 122, 255);">Alle akzeptieren
</span>
</div>
So I want to find the second element and tried things like:
self.browser.find_elements_by_xpath("//span[#style='the whole text that equals style in the html']")
But it does not seem to work...
Depending on what you would like to do with it, you could do //span[2] to select the second span:
Example of Selecting Element
self.browser.find_elements_by_xpath("//span[2]")
if you want the entire element.
Example of Selecting Text
self.browser.find_elements_by_xpath("//span[2]/text()")
if you want the inner html text of the second span.
I tested the xpath expression on this website to make sure that it works.
I copied in the html you provided and just entered the xpath expression //span[2]/text() to test it.
More info on xpath here.
Related
My program needs to copy into the clipboard some text with hyhperlinks, so that the user can later paste the text (with hyperlinks) into an app (e.g. Word, Slack).
This functionality works in most apps: for example if I copy a piece of text from a browser (or a Word doc, or a Slack message) which contains hyperlinks, when I paste it into another app it preserves the links.
However when I paste this content with pyperclip I only recover the text (there are no hyperlinks).
After some research I think I need to use a binary form of paperclip content, via pyperclip3. But I don't know how to construct the hyperlinked string object.
When I pyperclip3.paste() after copying the string from Word, I obtain what looks like a binary PDF object. Perhaps I need to create a binary PDF with the link and pass it to pyperclip3.copy()? Are there easier ways? Perhaps MIME?
Not sure if it is possible, but it's going to be very hard to use pyperclip to get hyperlinks, maybe even impossible. A possible option is to use Beautiful Soup and richxerox using Mac environment:
from richxerox import *
from bs4 import BeautifulSoup
soup = BeautifulSoup(pasteboard.get_contents(format='html'),'lxml')
links = [i.attrs['href'] for i in soup.find_all('a')]
print(links)
When copying to the clipboard in Windows, the application can choose to make several formats of the data available to an application that wishes to paste the data. I have attached a screenshot of the different formats available when copying from Chrome.
I know PySide6, a Python wrapper around Qt, lets you give the mime-type of the data you are putting into the clipboard. I don't know if there is a similar feature in pyperclip. This feature lets you share html with included hyperlinks directly with another application.
app.clipboard().mimeData().setHtml("""<html>
<body>
<!--StartFragment--><h3 class="s-post-summary--content-title" style="margin: -0.15rem 0px 0.3846rem; padding-top: 0px; padding-right: var(--su24); padding-bottom: 0px; padding-left: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; line-height: var(--lh-md); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI Adjusted", "Segoe UI", "Liberation Sans", sans-serif; font-size: var(--fs-body3); vertical-align: baseline; box-sizing: inherit; display: block; word-break: break-word !important; overflow-wrap: break-word !important; hyphens: auto !important; color: rgb(35, 38, 41); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
Copying hyperlinked text to the clipboard in Python pyperclip3</h3><div class="s-post-summary--content-excerpt" style="margin-top: calc(var(--su2) * -1); margin-right: 0px; margin-bottom: var(--su8); margin-left: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; line-height: inherit; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI Adjusted", "Segoe UI", "Liberation Sans", sans-serif; font-size: 13px; vertical-align: baseline; box-sizing: inherit; color: var(--fc-medium); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; word-break: break-word !important; overflow-wrap: break-word !important; hyphens: auto !important; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">
My program needs to copy into the clipboard some text with hyhperlinks, so that the user</div><!--EndFragment-->
</body>
</html>
""")
Update: Pyperclip "Currently only handles plaintext." So copying hyperlinks isn't an option currently with pyperclip. https://github.com/asweigart/pyperclip#:~:text=Currently%20only%20handles%20plaintext.
I am using an undetectable Chrome Selenium webdriver in python to try and capture all iframe elements within the div tag with the px-captcha id. The html section I am trying to get from the whole page source looks as such:
<div id="px-captcha" role="region" aria-label="Human challenge" style="display: block; min-width: 310px;">
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: block; width: 100%; height: 100px; border: 0; -moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; -ms-user-select: none; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<iframe style="display: none; width: 100%; height: 100px; border: 0px; user-select: none;" token="464ad1a7b7ed017f8c60542b859c5333648d76b8ec128a4f886c558900ffbe63dda2831825532a9be2d1f224eee47121e522391c7dcd7c55e86e1f17ebe944b7" title="Human verification challenge"></iframe>
<p id="ejjpkyuhrAUbSZp" role="alert" style="color: #e50000; display: inline-block; margin: 0; vertical-align: middle;"></p>
</div>
I manage to get the px-captcha id with:
element = driver.find_element(By.ID,'px-captcha')
This gives me a WebElement from which I want to get all the iframe elements. I tried it with this but it fails:
iframes = element.find_element(By.TAG_NAME,'iframe')
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"iframe"}
EDIT
The desired output is a list of the iframe WebElements similar to the type given by element = driver.find_element(By.ID,'px-captcha')
Almost all the descendant iframes are having:
style="display: none;
sans one.
To get the count of all the descendant iframes you need to induce WebDriverWait for the presence_of_all_elements_located() and you can use the following Locator Strategy:
element = driver.find_element(By.ID,'px-captcha')
print(len(WebDriverWait(element, 20).until(EC.presence_of_all_elements_located((By.TAG_NAME, "iframe")))))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try that (Python):
try:
iframes = driver.find_elements(By.TAG_NAME,'iframe')
for frames in iframes:
print(frames.get_attribute('....'))
except Exception as e:
raise e
(Replace '....' by what you want)
Remeber "find_elements" is array, so you need a for or something like that.
To interact with everything inside the frame you need:
driver.switch_to.frame(iframe)
You can return to main frame with:
driver.switch_to.default_content()
I have code like this.
...
<p style="font-size: 1.3rem; margin-top: 1.5rem; border: solid 1px #eaeaea; padding: 2rem; background-color: #efefff;">
{{ object.content|safe}}
</p>
...
I expected it would set style on object.content. But what I got is the content went below styled tag <p> not inside it. Because object.content also has its tag <p>.
So how do I set style using safe filter?
Wrap the paragraph tag with 'div' tag. Provide the font changes on that 'div' tag.
<div class="safe" style="font-size: 1.3rem; margin-top: 1.5rem; border: solid 1px #eaeaea; padding: 2rem; background-color: #efefff;">
<p>
{{ object.content|safe}}
</p>
</div>
This Will Work
I want to dynamically create an html code block but I'm not sure how to assign this into a variable:
<head>
<style>
body {
border-style: solid;
border-width: 5px;
border-color: green;
border-radius: 5px;
border-spacing: 30px;
background-color: #FFFFC2;
padding-bottom: 5px;
font-size: 25px;
}
li {
color: #F7270F;
font-weight: bold;
}
</style>
</head>
<body>
<div style="margin-bottom: 5px; margin-top: 5px; margin-left: 10px; margin-right: 10px;">
<h1 align="center">Auctions for Week of 08-01-2018</h1>
<p>You are bidding on the following item:</p>
<ul><li>This is the item for sale</li></ul>
<p>Condition is pack fresh unless otherwise indicated. Please review the pictures carefully and if you have any questions about something specific, ask.</p>
<p><b>Shipping:</b> Shipping will be calculated based on buyer location. No premiums are charged. Cards are mailed in an 8x5 bubble mailer and shipped First Class mail unless the price exceeds $50, at which point they will be shipped Priority at no additional cost to the buyer. If you win multiple auctions, please wait for an invoice to be sent.</p>
</div>
</body>
I've tried executing:
html_desc="<code_block>"
but it doesn't like the way that works... so I'm a bit stumped. The reason I'm trying to put this into a variable is because I need to make this a key value for a JSON statement.
You can put that code block inside triple quotes """ (I put the variable inside json.dumps, just for example):
from pprint import pprint
import json
html_desc = """<head>
<style>
body {
border-style: solid;
border-width: 5px;
border-color: green;
border-radius: 5px;
border-spacing: 30px;
background-color: #FFFFC2;
padding-bottom: 5px;
font-size: 25px;
}
li {
color: #F7270F;
font-weight: bold;
}
</style>
</head>
<body>
<div style="margin-bottom: 5px; margin-top: 5px; margin-left: 10px; margin-right: 10px;">
<h1 align="center">Auctions for Week of 08-01-2018</h1>
<p>You are bidding on the following item:</p>
<ul><li>This is the item for sale</li></ul>
<p>Condition is pack fresh unless otherwise indicated. Please review the pictures carefully and if you have any questions about something specific, ask.</p>
<p><b>Shipping:</b> Shipping will be calculated based on buyer location. No premiums are charged. Cards are mailed in an 8x5 bubble mailer and shipped First Class mail unless the price exceeds $50, at which point they will be shipped Priority at no additional cost to the buyer. If you win multiple auctions, please wait for an invoice to be sent.</p>
</div>
</body>"""
pprint(json.dumps({html_desc: "Some Value"}))
I was wondering if it was possible to change or remove the "display: none;" in the element to make it visible with selenium in python.
<textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid #c1c1c1; margin: 10px 25px; padding: 0px; resize: none; display: none; "></textarea>
I already tried this:
driver.execute_script("document.getElementById('g-recaptcha-response').style.display = '1';")
but apparently it does not work.
Any help would be apprreciated. :)
It was just the wrong CSS attribute, this works
driver.execute_script("document.getElementById('g-recaptcha-response').style.display = 'block';")
Thanks to #Andersson