简体   繁体   English

python-selenium,使用 xpath

[英]python-selenium, using xpath

Basically, I was trying to make macro program by python for buying product which is only available for only one day (it is normally sold out and only opens certain time.)基本上,我试图通过 python 制作宏程序来购买只有一天可用的产品(它通常售罄并且只开放特定时间。)

Additionally it is first time first served, hardly have a chance.此外,它是第一次服务,几乎没有机会。

So the problem is this:所以问题是这样的:

I was trying to use XPath and select elements, and then use if and else to distinguish whether I could buy it or not (based on the comparison of the products in the website which is available and not available.).我尝试使用 XPath 和 select 元素,然后使用ifelse来区分是否可以购买(基于网站中可用和不可用的产品的比较。)。

So I searched overlapped code in the page and those are found.所以我在页面中搜索了重叠的代码并找到了那些。


<1> the products now available looks like this <1> 现在可用的产品是这样的

<ul class="_3YA58cPPsy">

<li class="_3nAZvQO51p N=a:pcs.buy">

<a href="javascript:void(0)" class="OgETmrvExa">

<span class="blind">

구매하기

</span>
</a>
</li>
</ul>

<2> the product that is not open looks like this <2>未开封的商品是这样的

<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">

<336>

</em>
</a>
</li>
</ul>

So I was trying like those.所以我就这样尝试。

 while True:

    xpath='//ul[@class="_3YA58cPPsy"]'

    aa=driver.find_element_by_xpath(xpath).get_attribute(class)

    if aa== "OgETmrvExa"
    
       print=("Yes")    

       xpath="//ul[@class='_3YA58cPPsy']"

       driver.find_element_by_xpath(xpath).click()
 
       break

    else 
 
       Print=("no")
 
       driver.refresh()

       time.sleep(1)

Or, I was trying to use el.text one.或者,我试图使用el.text之一。

   while True:

     xpath='//ul[@class="_3YA58cPPsy"]'

     el=driver.find_element_by_xpath(xpath)

     if  el.text="구매하기"       #<- this text is the same in <1>'s text

         el.click()
        
         break 

     else :
         driver.refresh()

It is not working at all.它根本不起作用。 What did I do wrong?我做错了什么? How should I fix it?我应该如何解决它?

Additionally, I will add the both <1> and <2> link, which is non-English but you would see the green button is the 'buy' button.此外,我将添加 <1> 和 <2> 链接,这是非英语的,但您会看到绿色按钮是“购买”按钮。 <1>: https://smartstore.naver.com/coopnc/products/4871588151?NaPm=ct%3Dkl0z3145%7Cci%3Dcheckout%7Ctr%3Dppc%7Ctrx%3D%7Chk%3Df4d54ef22f2a5479043920e1957be34937fa2c1b <1>: https://smartstore.naver.com/coopnc/products/4871588151?NaPm=ct%3Dkl0z3145%7Cci%3Dcheckout%7Ctr%3Dppc%7Ctrx%3D%7Chk%3Df4d54ef22f2a5479043920e1957be34937fa2c1b

<2>: https://smartstore.naver.com/hwaflora/products/5192517936 <2>: https://smartstore.naver.com/hwaflora/products/5192517936

thanks a lot.多谢。

A few things:一些东西:

xpath='//ul[@class="_3YA58cPPsy"]'
aa=driver.find_element_by_xpath(xpath).get_attribute(class)
if aa == "OgETmrvExa":
    ...

In this section there are two errors:本节有两个错误:

  1. class has to be in quotes - "class" class必须用引号括起来 - "class"
  2. Your XPath is finding an element based on it's class, and then you are defining a variable to the name of that class (in this case "_3YA58cPPsy" ).您的 XPath 正在根据它的 class 查找一个元素,然后您正在为该 class 的名称定义一个变量(在本例中为"_3YA58cPPsy" 。) aa will always be "_3YA58cPPsy" , so your if statement will never return true. aa 将始终为"_3YA58cPPsy" ,因此您的 if 语句将永远不会返回 true。

In this case, it might be easier to search for the div above the ul, and see how many uls there are in that div:在这种情况下,可能更容易搜索 ul 上方的 div,并查看该 div 中有多少个 ul:

buy = driver.find_element_by_css_selector("div.XqRGHcrncz")
lists = buy.find_elements_by_tag_name("ul")
if len(lists) == 2:
    buy.click()

In your second version:在您的第二个版本中:

xpath='//ul[@class="_3YA58cPPsy"]'
el=driver.find_element_by_xpath(xpath)
if  el.text="구매하기" :
    ...

You run into a similar problem.你遇到了类似的问题。 The text you are trying to access isn't the text of the ul, but of the great-grandchild of the ul - //ul[@class="_3YA58cPPsy"]/li/a/span .您尝试访问的文本不是 ul 的文本,而是 ul 的曾孙 - //ul[@class="_3YA58cPPsy"]/li/a/span的文本。 I think the method I referenced above is easier than finding the specific elements to extract data.我认为我上面引用的方法比查找特定元素来提取数据更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM