Python button to change style of html div - python

I have a project in python that will hide a div when my icon is clicked and then collapse when it is clicked again. Right now I have the following html code
<img scr="/path/to/img"></img>
<div>
<p>Press the icon to see more stuff</p>
</div>
<div id="showOrHide" style="display: none;">
<p>one</p>
<p>two</p>
<p>three</p>
</div>
So my question is what is the best way to remove the style on the div with the id showOrHide when the user clicks on the image?
Thanks!

Python isn't best equipped to do what you're trying to accomplish - is there no way you can use Javascript? A simple onclick() http://www.w3schools.com/jsref/event_onclick.asp will get the job done in JS.

Related

How to select an input according to a parent sibling label

The input element that I have does not have anything to identify it because is my page is built through a react framework. So I want to select the appropriate input by its label above it. My HTML looks like the following:
<div>
<label>Project</label>
</div>
<div>
<div>
<input>
</div>
</div>
<div>
<label>Type</label>
</div>
<div>
<div>
<input>
</div>
</div>
If you try to perform an action on a label which has associated input element Playwright will automatically retarget the action to the input.
If the DOM structure is always the same you can try something like this
page.locator('div:has( > :text-matches("Project")) + div input')
Otherwise layout selector is your best bet.
You could try with layout selectors:
const div = page.locator('input:right-of(:text("Project"))');

How to distinguish 2 button base on their parent/ancestor element in Selenium (PYTHON)?

<a href="/google.com">
<div> ABC </div>
<span>
<button class="btn"> Show more </button>
<span/>
</a>
<div>
<div>ABC</div>
<span>
<button class="btn"> Show more </button>
<span/>
</div>
As you guys see here, we have 2 BUTTONS and I need to click to BUTTON whose ancestor is not <a/> tag, because if I click to button whose ancestor is tag, it will redirect me to other pages. So I don't want this behaviour.
The obvious solution is I can use absolute Xpath for this, but it's not a good way because HTML DOM or css structure could be changed so it'
s not stable.
So how can I distinguish 2 button, using ancestor or something related? I mean other better ways.
Thank you guys for helping me so much!
To select a button that has no a tag ancestor can be done with the following XPath:
//button[not(ancestor::a) and(contains(.,'Show more'))]

How do I select this dropdown button in Selenium?

I have this html :
<div class="dropdown float-right">
<button class="btn btn-default" type="button" id="export-button" data-toggle="dropdown">
<i class="the-download-export"></i>
Export
</button>
<div class="dropdown-menu dropdown-menu-right"><div class="dropdown-item action-export" data-format="csv" data-store="com" data-reverse-type="single"><i class="text-primary fa fa-upload" aria-hidden="true"></i>Download File</div>
This is a dropdown that has a few options. When the button is clicked, it collapses the dropdown selection and the divs appear(there are more divs for various data formats which i haven't added here for simplicity).
I have tried a few things in order to click the button with the csv data format, but everything has failed. Either not finding it or getting the div only, which is not interactible.
What is the proper way of selecting and clicking this dropdown collapsible button in Python ?
That's very little to work on.
I can recommend either checking whether you can get the data another way (e.g. api call when clicking the button) or something with x-path like
button = getFromXPath(".//button[#id='export-button']")
button.click()

Having trouble finding/clicking specific images within a div class through selenium

Practicing web scraping through selenium by opening user's dating profiles through a dating site. I need selenium to save a href link for every profile on the page but not sure how to go about selecting it since each link is different for every profile and image. All of the profiles start with the same two div class/style which is "member-thumbnail" and "position: absolute". Thank you for any help that you can offer.
<div class="member-thumbnail">
<div style="position: absolute;">
<a href="/Member/Details/LvL-Up">
<img src="//storage.com/imgcdn/m/t/502b24cb-3f75-49a1-a61a-ae80e18d86a0" class="presenceLine online">
</a>
</div>
</div>
Try using more general selector as follow
.member-thumbnail a
photo = browser.find_element_by_css_selector('.SELECTOR #GOES HERE').click()
So it should look something like that
photo = browser.find_element_by_css_selector('.member-thumbnail a').click()

Clicking button within span with no ID using Selenium

I'm trying to have click a button in the browser with Selenium and Python.
The button is within the following
<div id="generate">
<i class="fa fa-bolt"></i>
<span>Download Slides</span>
<div class="clear"></div>
</div>
Chrome's dev console tells me the button is within <span> but I have no idea how to reference the button for a .click().
Well, if you just want to click on an element without an id or name, I'd suggest three ways to do it:
use xpath:
driver.find_element_by_xpath('//*[#id="generate"]/span')
use CSS selector:
driver.find_element_by_css_selector('#generate > span')
Just try .find_element_by_tag_name() like:
driver.find_element_by_id('generate').find_elements_by_tag_name('span')[0]
Note that this way first try to get the generate <div> element by it's id, and then finds all the <span> elements under that <div>.
Finally, gets the first <span> element use [0].

Categories