How do i click this specific nav button with Selenium webdriver - python

Here is a copy/paste from the source
<li id="find-tab" data-qa="sidebar.find.tab" class="nav-tab active">
<span class="in"></span>
<p id="find-label">FIND</p></li>
I have tried:
driver.find_element_by_link_text("FIND").click()
and:
driver.find_element_by_css_se("nav-tab").click()
neither is working.
How do I just simply click on the nav button given that source code I pasted above?

As suggested by #Random Davis in the comments as well. You can use :
driver.find_element_by_id('find-label').click();
Note : Please make sure this would work appropriately for the cases when the find-label element is unique in the page source.
In case there are chances of them being in multiple places, you can create and use a list using :
findLink = driver.find_elements_by_id('find-label');
for links in findLink:
print links.click();
# do whatever you want and navigate back to access the next find-label element

Related

i have a question to select xpath in sellenium(python)

i was trying to click some button in the page but it changes when it is available so that mine is not working.
basically, normally it's the only one section but changes into one that contains multiple buttons. and i am aiming to click buying or another buttons when it shows but i kept failing.
when it's unavailable(to click buying or shipping button), it looks like this.
<div class="XqRGHcrncz">
<ul class="_3YA58cPPsy">
<li class="_3nAZvQO51p N=a:pcs.mylist">
<a href="javascript:void(0)" role="button" class="_3Dy-2NaoiG" aria-pressed="false">
<span class="_3nBu7xChUl"><span class="blind">찜하기</span></span>
<em class="_1c-2nfzJqH">13</em></a></li></ul></div>
but when it is available, buying button appears. everything is same but the starting from
li class, it changes a bit.
li class became
<li class="_3nAZvQO51p N=a:pcs.mylist">
and the rest changed too.
<a href="javascript:void(0)" class="OgETmrvExa">
<span class="blind">구매하기</span>
how can i make xpath to click the element that shows only available?
the main problem is that div is changing.
xpath is sometimes
//*[#id="content"]/div/div[2]/div[2]/fieldset/div[7]/ul[1]/li/a
but sometimes it is
//*[#id="content"]/div/div[2]/div[2]/fieldset/div[8]/ul[1]/li/a
so that the div[] is changing. i tried css selector to click it when it turned into new page,
buy=driver.find_css_selector(div.XqRGHcrncz)
lists = buy.find_elements_by_tag_name("ul")
if len(lists) == 2: buy.click()
but when the page is loaded to available, it is not working at all...
i was trying to use xpath like this,
while True:
lists = buy.find_elements_by_tag_name("ul")
if len(lists) == 2:
break
else:
print("구매불가")
driver.refresh()
driver.implicitly_wait(10)
and then
driver.implicitly_wait(10)
xpath='//*[#id="content"]/div/div[2]/div[2]/fieldset/div[8]/ul[1]/li/a'
driver.find_element_by_xpath(xpath).click()
but as i mentioned, the xpath is changing and there is no use. the div[number] <- this changes so that it does not working as it is wrong xpath.
what should i do?? i would be really appreciate if anyone helps me.
(just in case, this is my page that i want to click when it became available...
https://smartstore.naver.com/hwaflora/products/5192517936 thank you)
Try to find element by text, is it stable?구매하기
so the code looks like that:
driver.find_element(By.XPATH, "//span[text()='구매하기']"
or by preceding xpath feature:
driver.find_element(By.XPATH, "//a/preceding::span[text()='찜하기']")
The xpath of your element is
String button = "//li[contains(#class,'_3nAZvQO51p N')]/a"
Now, you mentioned that until it is available there are more elements below it, for example this:
String loading = "//li[contains(#class,'_3nAZvQO51p N')]/a//em"
You can wait until this element disappears. The syntax in Java is:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until((ExpectedConditions.invisibilityOfElementLocated(By.xpath(loading))));
Now you can find and click the button element

Selecting dynamic element using selenium and python

Could someone please tell me how can I select a dynamic element using selenium?
I would like to select the "limit-order" element.
<div class="tab-control" id="uniqName_0_85" widgetid="uniqName_0_85">
<span data-tab="market-order" class="tab-item tab-active">Market</span>
<span data-tab="limit-order" class="tab-item">Limit</span>
<span data-tab="stop-order" class="tab-item">Stop</span>
<span data-tab="stop_limit-order" class="tab-item">Stop Limit</span>
</div>
I tried this but no luck:
btn_limit_name_xpath = '//div[contains(#class,"tab-control")]/span[2]'
btn_limit = browser.find_element_by_xpath(btn_limit_name_xpath)
btn_limit.click()
What sometimes does the job for me is copy the full xpath instead of the shorter one.
If that doesn't work either, you could try and check this out.
They show you how you can use an xpath to find a specific piece of text and select the object in that way. So in your case you could try and find it by searching for 'limit'.

Can't dfferentiate between two selectors meant to perform one specific action

I've written a script in python in combination with selenium to click on a button named as follow located in a webpage. The thing is when I try with two different selectors, they both can click on the same button.
First selector:
"a[href$='/follow']"
Second one:
"a[href$='/follow'] > button"
Portion of relevant html:
<div class="nZSzR">
<h1 class="fDxYl">
some royal personality
</h1>
<span class="VerifiedBadge" title="Verified">
Verified
</span>
<a class="BY3EC" href="/accounts/follow" rel="nofollow">
<button class="L3NKy" type="button">
Follow
</button>
</a>
</div>
Which way I should stick to and why?
MITHU,
The css selector you have a[href$='/follow'], represent an anchor tag with href attribute that ends with /follow
second one a[href$='/follow'] > button , targets a button whose parent is anchor tag.
So, you can clearly see in second css, there is a dependency of a button. So, if you can go ahead with just first you should give priority to that.
For this HTML :
<a class="BY3EC" href="/accounts/follow" rel="nofollow">
<button class="L3NKy" type="button">
Follow
</button>
</a>
Clearly first css has less dependency.
If it's anchor tag then answer is no need of css selector.
You can directly go ahead with LINK_TEXT or PARTIAL_LINK_TEXT
Sample code :
continue_link = driver.find_element_by_link_text('Continue')
continue_link = driver.find_element_by_partial_link_text('Conti')
For more go through this official link.
So the > is a child combinator meaning you will only match the right hand side if it is a child of the left hand side.
Depending on how consistent pages are I would look for a faster selector and in this case go with a third option of
#react-root a
This is using your link: https://www.instagram.com/cristiano/?hl=en
An id and type selector will be faster than especially when using find_element_by_css_selector as you only need first match

Python Selenium: How do I click a link in a drop down menu with css selector?

The HTML looks like this:
<span class="MenuIcons searchButton"></span>
... (some stuff)
<a data-bind="" url="/ParagonLS/Search/Property.mvc/Index/1" tabdescription="RESIDENTIAL" subtabdescription="Criteria" subtabmaxallowed="3" targetex="" rel="" class=" SearchByClass1 " subtabgroup="true" subtabgroupadd="true" subtabstartindex="0" fullwindow="False" hideaddressbar="False">TEXT</a>
I can get to the span using:
driver.find_element_by_css_selector(".MenuIcons.searchButton")
But since the span is a drop down menu I need to get to the inner element, but don't know how since it has spaces around its class name. What do I do?
import time
driver.find_element_by_css_selector(".MenuIcons.searchButton").click()
time.sleep(1)
driver.find_element_by_partial_link_text("TEXT").click()
You can do this and click the link.
I suggest you to use xpath instead since the class contains space.
//a[contains(#class,'SearchByClass1')]
Text based search is also another possibility.
//a[.='TEXT']
Edit
Executing javascript since the element is hidden as per OP's comment
test = driver.execute_script("return document.querySelector(\"a[class*='SearchByClass1']\").innerHTML;");
print(test)
print
TEXT

clicking on a link with the same href value using selenium python

I have a html code that has two links but both the links have the same href value, but the onclick and the text are different.
I wasn't sure as to how to access the second link.
I tried using driver.find_element_by_link_text('text'), but I get a no such element found error.
<div id="member">
<"a href="#" onclick="add_member("abc"); return false;">run abc<"/a>
<br>
<"a href="#" onclick="add_member("def"); return false;">run def<"/a>
</div>
There are multiple options to get the desired link.
One option would be to get use find_element_by_xpath() and check onclick attribute value:
link = driver.find_element_by_xpath('//div[#id="member"]/a[contains(#onclick, "add_member(\"def\")")]')
link.click()
Another one would be to simply find both links and get the desired one by index:
div = driver.find_element_by_id('member')
links = div.find_elements_by_tag_name('a')
links[1].click()
Which option to choose depends on the whole HTML content. Hope at least one of two suggested solutions solves the issue.

Categories